gpt4 book ai didi

ios - cellForRowAtIndexPath : Is it XCode template correct?

转载 作者:行者123 更新时间:2023-11-28 19:16:34 24 4
gpt4 key购买 nike

这个问题是关于 cellForRowAtIndexPath 的。

我知道正确的方法是

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"];
return cell;

但是当我创建 Master-Detail 模板时,代码看起来像这样

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [[object valueForKey:@"timeStamp"] description];
}

编辑:问题不在于 configureCell atIndexPath。如您所见,这些字符串在 XCode 模板中不存在。

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] ;

XCode 模板是否正确?

最佳答案

模板代码正确。

从 iOS 5.0 开始,可以为单元格标识符注册一个 nib。每当表格需要一个单元格并且没有剩余的可重用单元格时,它就会实例化 Nib 并自行创建单元格。

如果这样做,dequeueReusableCellWithIdentifier: 总是返回一个单元格;您不必自己创建它。

注册nibs的API是:registerNib:forCellReuseIdentifier: .

关于ios - cellForRowAtIndexPath : Is it XCode template correct?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11685142/

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