ios - 导航返回后更新 UITableViewCell 的自定义子类的标签?

标签 ios objective-c uitableview uilabel

从推送导航返回后,我正在尝试更新自定义单元格上的其中一个标签的分数。

在我的父 UITableViewController 中,我有代码:

ViewDidLoad

//These are mutable strings
self.disciplineScoreString1 = [NSMutableString stringWithFormat:@""];
self.disciplineScoreString2 = [NSMutableString stringWithFormat:@""];
self.disciplineScoreString3 = [NSMutableString stringWithFormat:@""];

self.disciplineScoreArray = [[NSMutableArray alloc] init];
[self.disciplineScoreArray addObject:self.disciplineScoreString1];
[self.disciplineScoreArray addObject:self.disciplineScoreString2];
[self.disciplineScoreArray addObject:self.disciplineScoreString3];

到目前为止一切顺利。

cellForRowAtIndexPath

//this is the custom subclass of UITableViewCell
DayTableViewCell *cell = (DayTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

//this is the custom label
cell.disciplineScoreLabel.text = [self.disciplineScoreArray objectAtIndex:indexPath.row];

return cell;

到目前为止仍然很好。现在,3 个单元格中的标签均为空白。

我现在从第一个单元格转到 UIViewController,然后从 child UIViewController 返回,成功设置了 self.disciplineScoreString1 的字符串值到我在父 UITableViewController 中 NSLog 的 @"10"

现在如何更新第一个单元格的标签?我已尝试在 ViewWillAppear重新加载数据,但它不起作用。

谢谢

编辑

这是Child ViewController中的代码

viewWillDisappear:

[super viewWillDisappear:animated];

[self calculateDisciplineScore];

NSInteger currentVCIndex = [self.navigationController.viewControllers indexOfObject:self.navigationController.topViewController];

DisciplineTableViewController *parent = (DisciplineTableViewController *)[self.navigationController.viewControllers objectAtIndex:currentVCIndex];

parent.disciplineScoreString1 = self.disciplineScoreText;

最佳答案

您必须更改字符串,而不是修改它...考虑这个示例

NSMutableArray *array = [NSMutableArray array];
NSMutableString *disciplineScoreString1 = [NSMutableString stringWithFormat:@"original string"];
[array addObject:disciplineScoreString1];

[disciplineScoreString1 appendString:@" hello"]; // OK.. you're modifying the original string
NSLog(@"%@", [array objectAtIndex:0]);

disciplineScoreString1 = [NSMutableString stringWithFormat:@"new string"]; // WRONG!!
NSLog(@"%@", [array objectAtIndex:0]);

输出是:

2014-02-15 06:36:46.693 Hello[19493:903] original string hello
2014-02-15 06:36:46.694 Hello[19493:903] original string hello

第二个示例是错误的,因为您正在创建一个新字符串,而数组中的对象仍然指向旧的原始字符串。

编辑:

// try this
[parent.disciplineScoreString1 replaceCharactersInRange:NSMakeRange(0, parent.disciplineScoreString1.length) withString:self.disciplineScoreText];
// instead of 
parent.disciplineScoreString1 = self.disciplineScoreText;

关于ios - 导航返回后更新 UITableViewCell 的自定义子类的标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21797120/

相关文章:

ios - 几天后正在测试的 iPhone 应用程序崩溃

objective-c - removeObjectAtIndex 导致 "message sent to deallocated instance"

iphone - 设置 UITableViewCell 文本的宽度

ios - 从 TableView 中删除行并从sqlite数据库中删除记录

ios - 在后台退出时应用“崩溃”

ios - 方向灯

html - iPhone 未显示 HTML 选择 iOS 12.1.4 的完整选项

ios - 使用FBSDKShareDialog发布到facebook墙不需要提交审核?

objective-c - 在 objective-c 中获取显卡信息

ios - 将表格单元格委托(delegate)给其他表格 View