从推送导航返回后,我正在尝试更新自定义单元格上的其中一个标签的分数。
在我的父 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/