我应该如何将Subview添加到cell.contentView?

2024-01-11

A(当新创建单元格时):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        CGRect frame = CGRectMake(0, 0, 160, 50);
        UILabel *label = [[UILabel alloc] initWithFrame:frame];
        label.textAlignment = UITextAlignmentRight;
        label.text = @"9:00am";
        [cell.contentView addSubview:label];
        [label release];
    }

    return cell;
}

或 B(每次找到单元格时):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];    
    }

    CGRect frame = CGRectMake(0, 0, 160, 50);
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.textAlignment = UITextAlignmentRight;
    label.text = @"9:00am";
    [cell.contentView addSubview:label];
    [label release];

    return cell;
}

A还是B?谢谢!

UPDATE解决方案(感谢各位的解答):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

    static NSString *CellIdentifier = @"Cell";    
    UILabel *label;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        CGRect frame = CGRectMake(0, 0, 160, 50);
        label = [[UILabel alloc] initWithFrame:frame];
        label.textAlignment = UITextAlignmentRight;
        label.tag = 1;
        [cell.contentView addSubview:label];
        [label release];
    } else {
        label = (UILabel *) [cell viewWithTag:1];
    }

    label.text = [NSString stringWithFormat:@"%d", [indexPath row]];

    return cell;
}

一切都与性能有关。对于 A,您可以重用单元格及其所有子视图,对于 B,您可以仅重用原始单元格并在每次迭代中添加一个新的子视图,恕我直言,这不如 A re: 性能。

我说要么创建一个UITableView子类化或使用解决方案 A。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我应该如何将Subview添加到cell.contentView? 的相关文章

  • 查找 iPhone 上已安装应用程序的列表

    是否可以以编程方式查找我的 iOS 设备上安装的所有应用程序的名称 有没有可用的API 谢谢您的帮助 不可以 由于沙盒环境 iOS 应用程序无法访问其他应用程序的信息
  • UILabel 文本的大纲

    如何向白色 UILabel 文本添加黑色轮廓 一种选择是设置阴影 这可能不完全是您想要的 但可以达到类似的效果 您可以手动调整偏移量 UILabel myLabel lbl shadowColor UIColor whiteColor lb
  • 是否可以自定义区域形状?核心位置

    例如 至少我想要一个正方形 矩形 道路的形状作为我的区域 不它不是 根据文档 目前唯一的区域类是 CLCircularRegion 显然是圆形 和 CLBeaconRegion 基于与 iBeacon 的接近度 未来苹果可能会添加对自定义形
  • dispatch_async 在某个地方有滞后,找不到哪里。 NSLog有问题吗?

    所以我有这个代码 dispatch async dispatch get global queue DISPATCH QUEUE PRIORITY DEFAULT 0 Bunch of code NSLog Test 它立即运行并返回 ns
  • 如何检测和处理UIWebView中的HTTP错误代码?

    我想在收到 HTTP 错误 404 等时通知用户 我怎样才能检测到这一点 我已经尝试过实施 void webView UIWebView webView didFailLoadWithError NSError error 但当我收到 40
  • Core Data 与 Sqlite 以及性能

    我想知道是否有人有关于已在 3 0 SDK 中转换为 Core Data 的旧 sqlite 应用程序之间的性能差异的真实数据 我怀疑核心数据在底层只是使用 sqlite 并且为了方便成本而增加了一些开销 但我想我还是会问 Core Dat
  • 从 NSArray 为 UITableView 创建索引

    我读过创建索引 uitableview 旁边的 a z 的最佳方法是设置一个 nsdictionaries 数组 其中每个字典对应一个部分 并且 rowValue 键包含一个行数组 NSDictionary headerTitle gt A
  • 训练 tesseract 与 iPhone 一起使用

    我正在尝试在我的 iPhone 应用程序中使用 tesseract 2 04 只想检测数字 我在这里所做的首先是使用这篇文章交叉编译 tesseract 以生成 lib 文件http robertcarlsen net 2009 07 15
  • iOS CoreBluetooth:状态保存和恢复

    希望在这里得到一些意见 在我当前的 iOS 项目中 我使用 CoreBluetooth 和 swift 该应用程序可以在后台使用 CoreBluetooth 进行通信 这基本上可以工作 外设需要与 iOS 设备建立有效连接才能按预期工作 每
  • 有没有用于iPhone开发的高级手势库?

    iPhone 平台有许多常见的手势习惯用法 例如 有轻击 捏合和滑动等操作 每种操作都需要不同数量的手指 但是 当您开发应用程序时 您需要根据有关触摸次数和位置的低级信息来实现这些内容 看起来这是图书馆的主要候选者 您将注册一个委托 设置一
  • Xcode 8 - 删除了一些按钮边框

    我刚刚将 Xcode 版本从 7 3 更新到 8 0 一些按钮边框消失了 代码看起来很好 所以我真的不知道各层发生了什么 顺便说一句 在其他一些控制器中我可以看到图层边框 self button layer borderColor bord
  • WKWebView 上的链接点击侦听器?

    WKWebView 类中是否存在类似 onLinkClickListener 的东西 我尝试用谷歌搜索但一无所获 我还在类似类型的 stackoverflow 上发现了一些未解答的问题 我需要 linkClickListener 的原因是
  • 检测iOS设备类型

    在我的应用程序 用 Objective C 编写 中 如何检测设备是 iPhone iPad 还是 iPhone5 if UIDevice currentDevice userInterfaceIdiom UIUserInterfaceId
  • 在 UIScrollView 中滚动而不触发 TouchCancelled

    Overview 我正在开发一款 iPhone 游戏 其代码是从另一位开发人员那里继承的 游戏网格是一个 contentSize 为 1000x1000 的 UIScrollView 网格包含通过 OpenGL 的 Texture2D 类在
  • CGContextSetLineWidth(context, 1) - 宽度几乎总是至少 2 像素而不是 1

    With CGContextSetLineWidth context 1 宽度几乎总是至少 2 像素而不是 1 QQCandleStickLayer m id init self super init if self nil self de
  • 可达性reachabilityWithAddress不起作用

    我正在尝试使用以下代码确定 iPhone 是否可以连接到我的 IP 地址 struct sockaddr in server address server address sin len sizeof server address serv
  • iPhone 4G 拍照时应用程序崩溃?

    我已从我的设备中拍摄照片并调整图像大小并将其设置到我的图像视图框架 但我的应用程序在拍摄照片后有时会崩溃 使用调整大小 现在我已经用 iPhone 4G 拍了这张照片 仅当许多应用程序在后台运行 多任务 时 应用程序才会崩溃 那么我该如何解
  • 高度在 IOS (iphone) 上无法正常工作

    我已经创建了this https codepen io salman15 project live DWbWpo Codepen 上的网站 在尝试使其响应所有平台时 我遇到了问题 看起来单个 div 覆盖了整个页面 仅在 IOS 上 并且并
  • 签名仅对临时无效

    我不确定我的临时项目发生了什么变化 但在尝试安装时出现此错误 应用程序未通过协同设计验证 签名无效 或者不是用Apple提交证书签名的 19011 设备调试构建良好 与我的临时配置文件关联的证书直到 2011 年才会过期 我搜索了 Goog
  • 是否可以在“NSFetchRequest”中按子类排序而不添加其他属性?

    我想对结果进行分组NSFetchRequest按实体 这些实体都共享相同的抽象父级 例如 animal cat dog The NSFetchRequest has includesSubentities set TRUE and enti

随机推荐