OGeek|极客世界-中国程序员成长平台

标题: objective-c - UITableView 中单元格的右 CellIdentifier [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 20:06
标题: objective-c - UITableView 中单元格的右 CellIdentifier

谁能解释一下两者的区别

static NSString* CellIdentifier = @"Cell";

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

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



Best Answer-推荐答案


static NSString* CellIdentifier = @"Cell";

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

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

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

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

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

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

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

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4