gpt4 book ai didi

objective-c - UITableView 中单元格的正确 CellIdentifier

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:50:21 26 4
gpt4 key购买 nike

谁能解释一下

static NSString* CellIdentifier = @"Cell";

NSString *CellIdentifier = [NSString stringWithFormat: @"Cell%i", indexPath.row];

我应该什么时候使用第一个,在哪里使用第二个?

最佳答案

static NSString* CellIdentifier = @"Cell";

此标识符(假设没有其他标识符)将标识一个单元格池,所有行在需要新单元格时将从中提取。

NSString *CellIdentifier = [NSString stringWithFormat: @"Cell%i", indexPath.row];

此标识符将为每一行创建一个单元池,即它将为每一行创建一个大小为 1 的池,并且该单元将始终仅用于该行。

通常您会希望始终使用第一个示例。第二个示例的变体如下:

NSString *CellIdentifier = [NSString stringWithFormat: @"Cell%i", indexPath.row % 2];

如果您希望每隔一行都具有特定的背景颜色或类似的东西,这将很有用。

如何从 here 正确设置单元格创建的示例:

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

if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

NSDictionary *item = (NSDictionary *)[self.content objectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:@"mainTitleKey"];
cell.detailTextLabel.text = [item objectForKey:@"secondaryTitleKey"];
NSString *path = [[NSBundle mainBundle] pathForResource:[item objectForKey:@"imageKey"] ofType:@"png"];
UIImage *theImage = [UIImage imageWithContentsOfFile:path];
cell.imageView.image = theImage;

return cell;
}

关于objective-c - UITableView 中单元格的正确 CellIdentifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10016035/

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