gpt4 book ai didi

ios - 分配和初始化 UITableViewCell 的子类有哪些问题

转载 作者:行者123 更新时间:2023-12-01 19:01:28 25 4
gpt4 key购买 nike

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

cell.firstLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
cell.secondLabel.text = [NSString stringWithFormat:@"%d", NUMBER_OF_ROWS - indexPath.row];

return cell;
}

这是来自 Apple Table View Programming Guide 的代码片段
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];工作正常,不需要检查 nil,因为单元格是在 Storyboard中定义的,并且总是返回有效的单元格。

但是,如果我不使用 Storyboard,我将如何以编程方式在我的表格 View 中使用多个自定义单元格?以及涉及哪些问题 allocating and initializing MyTableViewCell

最佳答案

你应该使用方法

- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier

UITableView 的。您可以阅读文档 here .

当你调用方法时
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier

,它检查重用队列中是否有可用的单元格。如果没有,它会检查它是否可以自动创建这个单元格。如果您之前为此重用标识符注册了单元格类或 nib,它将使用类或 nib 创建新单元格并返回它。如果你没有注册任何东西,它将返回 nil。

最好使用注册,因为如果您有不同的自定义单元格用于不同的重用标识符,则创建这些单元格的代码会变得困惑。这也是正确的方法。 iOS5和iOS6分别增加了注册方法。程序员创建自定义单元格的代码与旧版本的 iOS 有关。

关于ios - 分配和初始化 UITableViewCell 的子类有哪些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22659670/

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