当前位置: 代码迷 >> Iphone >> 这两个cell有什么区别呢,该怎么解决
  详细解决方案

这两个cell有什么区别呢,该怎么解决

热度:23   发布时间:2016-04-25 06:03:19.0
这两个cell有什么区别呢
- (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;
}
ios tableview

------解决方案--------------------
引用:
引用:引用:引用:1)
NSString* data=@"123"; //常量字符串
NSString* data=[NSString alloc]init…… //初始化NSString,方法为NSString的成员函数,根据自己的需要手动指定是否autor……


可以参考下这里的Creating Strings说明:https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Strings/Articles/CreatingStrings.html

字符串常量的话,在编译程序时就已经确定好了字符空间大小,程序加载时就会分配好对应的地址,程序关闭时才会释放,整个程序运行过程中不用去释放。
而alloc出来的NSString则是动态分配与释放。
------解决方案--------------------
引用:
引用:引用:引用:很简单,说白了,第一个cell是可充用的,但第二个cell就是每次都要进行初始化一次的。就这么简单,而用tableview的目的就是为了cell重用,故而,为啥要用第二种方式呢?


我每次都写成第一种形式的,,,但是每……


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