• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

objective-c - UITableView 中单元格的右 CellIdentifier

[复制链接]
菜鸟教程小白 发表于 2022-12-12 20:06:12 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

谁能解释一下两者的区别

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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap