gpt4 book ai didi

ios - dequeueReusableCellWithIdentifier 从不返回 nil

转载 作者:IT王子 更新时间:2023-10-29 08:05:14 26 4
gpt4 key购买 nike

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

- (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 开始,当您使用 Storyboard并且您的重用标识符与 Storyboard中的原型(prototype)匹配时,您将不会从 dequeueReusableCellWithIdentifier 返回 nil。

来自 Apple 文档:

Table View Programming Guide for iOS

Creating and Configuring a Table View

Populating a Dynamic Table View with Data

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.

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

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

最好使用断点来记录它。

enter image description here

$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>>

如果你只想要地址

enter image description here

0x097f9850
0x0a6a4a00
0x0a3ad250
0x0a3ae640
0x0972a370

关于ios - dequeueReusableCellWithIdentifier 从不返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16382068/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com