dequeueReusableCellWithIdentifier 从不返回 nil [英] dequeueReusableCellWithIdentifier never returns nil
问题描述
我在 UITableView 中使用自定义 UITableViewCell,但问题是在调用 dequeueReusableCellWithIdentifier 时单元格永远不会为零.这是为什么?
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 文档:
From Apple Doc:
iOS 表视图编程指南
Table View Programming Guide for iOS
创建和配置表视图
用数据填充动态表视图
如果 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屋!