iOS:如何正确访问 UITableViewCell 的 contentView 上的自定义标签?

2023-11-30

我有一个 UITableView,每个单元格被分成几个部分,每个部分都有不同的标签。该表由 NSDictionaries 的 NSArray 填充,其中包含填充单元格标签的所有数据。 UITableView 的这一部分效果很好。

当我更改其中一个 NSDictionaries 中的某些值,然后使用更新后的 NSArray 重新加载表时,就会出现问题。通常,当我打电话时[myTableView reloadData];即使(通过调试)我可以看到正在处理更新的数据,也没有更新任何内容。但如果我改变标准:if (cell == nil) { to if (1) {, 在里面cellForRowAtIndexPath方法,那么它的效果就很漂亮。我明白为什么if(1) {有效,但我不明白为什么我不能重用单元格而只能更改标签文本。

为什么if (cell == nil)不行?重新初始化每个单元是否会消耗巨大的资源?

CODE:

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

static NSString *cellIdentifier = @"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];

if (/*cell == nil*/1) {
    // Initialize Custom Cell
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    //// Background View
    cellBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 622, 43)];
    [cellBackgroundView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Images/Library/phase_table_cell.png"]]];

    //// Name Label
    nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 18)];
    [nameLabel setBackgroundColor:[UIColor clearColor]];
    [cellBackgroundView addSubview:nameLabel];

    //// Percent Complete Label
    percentCompleteLabel = [[UILabel alloc] initWithFrame:CGRectMake(300, 10, 30, 18)];
    [percentCompleteLabel setBackgroundColor:[UIColor clearColor]];
    [percentCompleteLabel setTextAlignment:UITextAlignmentCenter];
    [cellBackgroundView addSubview:percentCompleteLabel];

    //// Overall Status Label
    overallStatusLabel = [[UILabel alloc] initWithFrame:CGRectMake(352, 7, 63, 30)];
    [overallStatusLabel setBackgroundColor:[UIColor clearColor]];
    [overallStatusLabel setFont:[UIFont boldSystemFontOfSize:12.0]];
    [overallStatusLabel setLineBreakMode:UILineBreakModeWordWrap];
    [overallStatusLabel setNumberOfLines:2];
    [overallStatusLabel setTextAlignment:UITextAlignmentCenter];
    [cellBackgroundView addSubview:overallStatusLabel];

    //// Finish Date Label
    finishDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(425, 10, 55, 18)];
    [finishDateLabel setBackgroundColor:[UIColor clearColor]];
    [finishDateLabel setTextAlignment:UITextAlignmentCenter];
    [finishDateLabel setFont:[UIFont systemFontOfSize:12.0]];
    [cellBackgroundView addSubview:finishDateLabel];

    //// Overall Weight Label
    overallWeightLabel = [[UILabel alloc] initWithFrame:CGRectMake(505, 10, 30, 18)];
    [overallWeightLabel setBackgroundColor:[UIColor clearColor]];
    [overallWeightLabel setTextAlignment:UITextAlignmentCenter];
    [cellBackgroundView addSubview:overallWeightLabel];

    //// Green Risk View
    greenRiskView = [[UIView alloc] initWithFrame:CGRectMake(557, 4, 61, 10)];
    [greenRiskView setBackgroundColor:[UIColor greenColor]];
    [greenRiskView setHidden:YES];
    [cellBackgroundView addSubview:greenRiskView];

    //// Yellow Risk View
    yellowRiskView = [[UIView alloc] initWithFrame:CGRectMake(557, 17, 61, 10)];
    [yellowRiskView setBackgroundColor:[UIColor yellowColor]];
    [yellowRiskView setHidden:YES];
    [cellBackgroundView addSubview:yellowRiskView];

    //// Red Risk View
    redRiskView = [[UIView alloc] initWithFrame:CGRectMake(557, 30, 61, 10)];
    [redRiskView setBackgroundColor:[UIColor redColor]];
    [redRiskView setHidden:YES];
    [cellBackgroundView addSubview:redRiskView];

    [cell.contentView addSubview:cellBackgroundView];
}

// Get Current Dictionary
NSDictionary *dictForIndexPath = [self.phaseArray objectAtIndex:[indexPath row]];
// Set Elements
[nameLabel setText:[dictForIndexPath objectForKey:@"name"]];
[percentCompleteLabel setText:[dictForIndexPath objectForKey:@"percentComplete"]];
[overallStatusLabel setText:[dictForIndexPath objectForKey:@"overallStatus"]];
[overallWeightLabel setText:[[NSNumber numberWithInt:[[dictForIndexPath objectForKey:@"overallWeight"] intValue]] stringValue]];
//// Create Finish Date String
NSString *finishDateString = [NSString stringWithFormat:@"%@/%@/%@", [dictForIndexPath objectForKey:@"finishDay"], [dictForIndexPath objectForKey:@"finishMonth"], [dictForIndexPath objectForKey:@"finishYear"]];
[finishDateLabel setText:finishDateString];
//// Pick Risk View
NSString *riskColor = [dictForIndexPath objectForKey:@"riskColor"];
if ([riskColor isEqualToString:@"Green"]) {
    [greenRiskView setHidden:NO];
    [yellowRiskView setHidden:YES];
    [redRiskView setHidden:YES];
} else if ([riskColor isEqualToString:@"Yellow"]) {
    [greenRiskView setHidden:YES];
    [yellowRiskView setHidden:NO];
    [redRiskView setHidden:YES];
} else {
    [greenRiskView setHidden:YES];
    [yellowRiskView setHidden:YES];
    [redRiskView setHidden:NO];
}

return cell;
}

可能您正在分配范围内的值if(cell==nil)堵塞?只有初始化应该发生在该块内。将其余部分移出。 (发布您的完整cellForRow代码,如果您需要更多帮助)

//编辑:现在,在您发布代码后,我看到您的问题:

您将所有标签和视图存储在成员变量中..但是当然,只有当if(cell != nil)块被执行。此后,您始终访问同一个单元格(最后分配的)。所以您至少要更新一个单元格;)

要解决您的问题,请工作,例如使用标签从单元格中获取相应的视图。我将为您的背景视图显示它,但您必须为所有视图执行此操作(而不是成员变量。删除它们。)

static NSString *cellIdentifier = @"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];

if (cell == nil) {
  //// Background View
  UIView* cellBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 622, 43)];
  cellBackgroundView.tag = 1;
  [cellBackgroundView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Images/Library/phase_table_cell.png"]]];
}

// get the backgroundview from the current cell
UIView* backgroundView = [cell.contentView viewWithTag: 1];

等等..

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

iOS:如何正确访问 UITableViewCell 的 contentView 上的自定义标签? 的相关文章

  • 播放(非库)Apple Music 内容 - 请求失败

    我正在尝试使用以下代码播放专辑 let predicate MPMediaPropertyPredicate value 1459938538 forProperty MPMediaItemPropertyAlbumPersistentID
  • 获取 Cocoa 中文件的类型

    我成功找到了指定文件的扩展文件类型 JPEG 图像 TIFF 图像等 但我正在寻找更通用的东西 可以对 大类别 中的文件进行分类 如图像 moovies 文本文件等 有没有办法在可可 或 Objective C 中实现这一点 感谢您的帮助
  • Apple 帮助创作

    我正在跟进本指南 http developer apple com library mac documentation Carbon Conceptual ProvidingUserAssitAppleHelp authoring help
  • 检测 UITableView 中的水平平移

    我正在使用 UIPanGestureRecognizer 来识别 UITableView 中的水平滑动 准确地说是在单元格上 尽管它已添加到表格本身 然而 这个手势识别器显然窃取了桌子上的触摸 我已经让 pangesturerecogniz
  • UIWebView 未正确加载 JavaScript - 嵌入式 Facebook 帖子

    Facebook 有一项新功能 允许用户将公共帖子嵌入网页中 我想尝试在 UIWebView 内的 iPhone 应用程序中使用它 转义必要的代码非常简单 但即使我手动转义代码 Web 视图也不会正确加载帖子 JavaScript 根本不起
  • 在 Objective-C 中获取对象的属性数组

    Objective C 中是否可以获取对象所有属性的数组 基本上 我想做的是这样的 void save NSArray propertyArray self propertyNames for NSString propertyName i
  • 删除部分(红色删除按钮),UITableViewController - iOS

    我正在尝试做一个分组的 uitableview 并且我已经激活了编辑选项 我希望用户也能够删除整个部分 而不仅仅是特定行 因此 当您单击 编辑 时 每个表格单元格左侧显示的红色减号按钮也应该显示在各个部分 部分标题左侧 有人知道如何做到这一
  • iPhone ImageView序列动画

    嘿 尝试将一个简单的 png 序列动画放入我的应用程序中 我在 IB 中放置了第一个框架 并将图形动画出口连接到它 序列中有 54 个 png 名称为 Comp 1 0000 png 到 Comp 1 00053 png 这是我的代码 vo
  • Firebase Messaging FCM 在可配置的时间间隔内分发

    当您使用 FCM 向给定应用程序的所有设备发送推送时 这可能会导致许多用户同时打开他们的应用程序 从而导致大量服务器轮询 从而导致负载峰值 有没有一种方便的方法可以在给定的时间间隔内分发消息以进行计划推送 最后 我们找到了一种可能的方法 通
  • 防止 UITableView 滚动到某个点以下

    如何让 UITableView 允许在某个索引行上方滚动 但在低于某个点时阻止滚动 例如 如果我有第 1 行到第 100 行 其中在给定时间视图中仅出现 5 行 我希望允许用户在第 1 50 行之间滚动 但在第 50 行可见时阻止进一步向下
  • 实时获取 Apple Watch heartRateVariabilitySDNN 吗?

    我正在使用下面的函数来获取 heartRateVariabilitySDNN 但它只获取一次并且不能像 heartbeat 那样实时计算 func HRVstart guard let quantityType HKObjectType q
  • NSPredicate 使用 RLMResults 作为参数

    我试图通过使用 NSPredicate 进行过滤来获取两组 Realm 数据 并且是不同的对象 之间的差异 但存在一个我无法理解的错误 我的代码 RLMResults topStories KFXTopStory allObjects NS
  • 以编程方式添加带有自动布局的 UISLider

    我正在尝试以编程方式将 UISlider 添加到我的视图中 包括约束 以便其宽度适应整个屏幕宽度 这是我到目前为止得到的 2 Add UISlider self slider UISlider alloc init self view ad
  • ios - 使用 SIGPIPE 和 SIG_IGN 的信号函数

    我加入了一个旧项目 我发现了这条线 BOOL application UIApplication application didFinishLaunchingWithOptions NSDictionary launchOptions si
  • 我正在寻找 GCDAsyncUdpSocket 上的一些示例,但发现没有一个有效

    接收数据从未被调用过 我编写了这个由我的主线调用的 swift 类UI视图控制器向接收消息的服务器发送消息 但当服务器发回响应时 客户端永远不会收到它 因为 didReceiveData 从未被触发 我一直在谷歌上搜索并查看文档 它说客户端
  • swift 3.0 中的 Sha 256 加密语法错误

    func SHA256 gt String let data self data using String Encoding utf8 let res NSMutableData length Int CC SHA256 DIGEST LE
  • iPhone 上的锁定方向 UIWebView

    有没有办法锁定 UIWebView 的方向 使用 Obj C JS 还是 Html 我不想有按钮或任何东西 我只想在应用程序打开时将其锁定为纵向 好像这个堆栈溢出帖子 https stackoverflow com questions 43
  • 使用排序函数按 NSDates 对数组进行排序[重复]

    这个问题在这里已经有答案了 我有一个名为的模型类Event import Foundation import MapKit public class Event let id Int var title String let status
  • 将我的免费应用程序从 Universal 升级到仅限 iPhone

    我释放我的free app到 appStore 它的版本是 1 0 它是一个Universal app 现在我想发布 1 1 版本到 appStore 我将其升级到iPhone only appStore会拒绝我吗 我已阅读类似的问题 ht
  • NSUserDefaults、Settings.bundle 和应用程序组

    我有一个有 2 个目标的应用程序 主应用程序和 Today 扩展 为了在这些目标之间共享设置 我打开了应用程序组功能 添加了一个组group myApp com然后使用NSUserDefaults在主应用程序和今日扩展中都是如此 var d

随机推荐