- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"RecipeCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    
    cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
    return cell;
}-----------------------------------------------------------
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
    
    return cell;
}
iostableview

解决方案 »

  1.   

    还有就是tableview数据多了,滑动的时候为什么回崩溃,怎么办呢??
      

  2.   

    第一种创建的方法cell可以重复利用,例如:页面显示5个cell,实际有10条数据,第6个数据出现时第1个数据在上面隐藏了,但不需要再创建一个cell,可以利用第1个的cell了,这种方法10个数据创建了5个cell,减少很多cell创建;
    第二种创建方法就是每一个数据都创建一个cell,例如:页面显示5个cell,实际有10条数据,第6个数据出现时又创建了一个cell,第一个cell却放着不用,这种方法10个数据创建了10个cell,每次只显示5个,另外5个放着不显示。。浪费空间
      

  3.   

    NSString* data=@"123";
    NSString* data=[NSString alloc]init…………
    NSString* data=[NSString stringwith……
    这三个有什么区别呢??
      

  4.   

    一个页面上显示两个tableview该如何显示~~~不能用分组哦。
    该怎么判断是第一个tableview还是第二个tableview呢??
      

  5.   

    1)
    NSString* data=@"123"; //常量字符串
    NSString* data=[NSString alloc]init…… //初始化NSString,方法为NSString的成员函数,根据自己的需要手动指定是否autorelease
    NSString* data=[NSString stringwith…… //同上,只是方法为NSString的静态方法,且默认为autorelease的
    2)UIView有个tag属性,可以根据不同的tableview设置tag进行区分。
      

  6.   


    你二种是这样的,以下两个写法都一样,根据自己的使用习惯:
    NSString* data=[[NSString alloc] init];
    ...//do something
    [data release];
    或者
    NSString* data=[[[NSString alloc] init] autorelease];
    ...//do something
      

  7.   


    那前两种,就是字符常亮串和alloc的,生成的对象在内存里,都放在哪呢?
      

  8.   


    可以参考下这里的Creating Strings说明:https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Strings/Articles/CreatingStrings.html字符串常量的话,在编译程序时就已经确定好了字符空间大小,程序加载时就会分配好对应的地址,程序关闭时才会释放,整个程序运行过程中不用去释放。
    而alloc出来的NSString则是动态分配与释放。
      

  9.   

    很简单,说白了,第一个cell是可充用的,但第二个cell就是每次都要进行初始化一次的。就这么简单,而用tableview的目的就是为了cell重用,故而,为啥要用第二种方式呢?
      

  10.   


    你的写法应该更可靠些,不过如果不是ARC环境的话,你的cell alloc的时候忘了最后加autorelease;
      

  11.   


    谢谢指点。我没用过ARC,问一下,平时你们写程序是用ARC多还是不用ARC多呢??
      

  12.   


    非ARC的情况多吧,一方面方便兼容低版本的IOS系统版本,另一方面也好自己控制内存,查内存泄露什么的。