dequeueReusableCellWithIdentifier永远不会返回nil [英] dequeueReusableCellWithIdentifier never returns nil

查看:121
本文介绍了dequeueReusableCellWithIdentifier永远不会返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UITableView中使用自定义UITableViewCell,但问题是在调用dequeueReusableCellWithIdentifier时,单元格永远不会为nil。这是为什么?

I am using a custom UITableViewCell in my UITableView but the problem is that the cell is never nil when calling the dequeueReusableCellWithIdentifier. Why is this ?

- (void)viewDidLoad
{
    [super viewDidLoad];
    UINib *nib = [UINib nibWithNibName:@"PHResultTableViewCell" bundle: nil];
    [[self tableView] registerNib:nib forCellReuseIdentifier:@"MyCell"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
    PHResultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
    if (cell == nil)
    {
        PackageHolidayItem *obj=[[PackageHolidayItem alloc]init];
        obj= [totalArray objectAtIndex:indexPath.row];
        cell.packageHolidayItem = obj;
        [cell loadRow];

    }
    return cell;
}    


推荐答案

从iOS 5开始使用故事板和您的重用标识符匹配故事板中的原型,您将无法从dequeueReusableCellWithIdentifier返回nil。

Starting in iOS 5 when you use storyboards and your reuse identifier matches a prototype in your storyboard you will not get a nil returned from dequeueReusableCellWithIdentifier.

来自Apple Doc:

From Apple Doc:


iOS视图编程指南

Table View Programming Guide for iOS


创建和配置表格视图

Creating and Configuring a Table View


填充动态带数据的表视图

Populating a Dynamic Table View with Data

如果dequeueReusableCellWithIdentifier:方法要求在故事板中定义的单元格
,则该方法始终返回有效的
单元格。如果没有等待重复使用的回收单元,则方法
使用故事板本身中的信息创建一个新单元。这个
消除了检查nil的返回值并手动创建
单元格的需要。

If the dequeueReusableCellWithIdentifier: method asks for a cell that’s defined in a storyboard, the method always returns a valid cell. If there is not a recycled cell waiting to be reused, the method creates a new one using the information in the storyboard itself. This eliminates the need to check the return value for nil and create a cell manually.



您可以记录单元格地址以向您自己证明它们正在被重用。但是不要附带日志记录它会真正减慢你的表。

You can log the cell address to prove to your self they are being reused. But don't ship with the logging it will really slow up your table.

NSLog(@"Deque Cell %p", cell);

更好的是使用断点来记录它。

Better yet use breakpoint to log it.

$25 = 0x097f9850 <DDSImageSubtitleCheckedTableViewCell: 0x97f9850; baseClass = UITableViewCell; frame = (0 22; 320 44); hidden = YES; autoresize = W; layer = <CALayer: 0x97f9740>>
$26 = 0x0a6a4a00 <DDSImageSubtitleCheckedTableViewCell: 0xa6a4a00; baseClass = UITableViewCell; frame = (0 66; 320 44); hidden = YES; autoresize = W; layer = <CALayer: 0xa6a4b50>>
$27 = 0x0a3ad250 <DDSImageSubtitleCheckedTableViewCell: 0xa3ad250; baseClass = UITableViewCell; frame = (0 110; 320 44); hidden = YES; autoresize = W; layer = <CALayer: 0xa3ad390>>
$28 = 0x0a3ae640 <DDSImageSubtitleCheckedTableViewCell: 0xa3ae640; baseClass = UITableViewCell; frame = (0 176; 320 44); hidden = YES; autoresize = W; layer = <CALayer: 0xa3ae780>>
$29 = 0x0972a370 <DDSImageSubtitleCheckedTableViewCell: 0x972a370; baseClass = UITableViewCell; frame = (0 220; 320 44); hidden = YES; autoresize = W; layer = <CALayer: 0x972a340>>

如果您只想要地址

0x097f9850
0x0a6a4a00
0x0a3ad250
0x0a3ae640
0x0972a370

这篇关于dequeueReusableCellWithIdentifier永远不会返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆