Ios 导航返回后是否更新UITableViewCell自定义子类的标签?
从推送导航返回后,我正在尝试更新自定义单元格上某个标签的分数 在我的家长Ios 导航返回后是否更新UITableViewCell自定义子类的标签?,ios,objective-c,uitableview,uilabel,Ios,Objective C,Uitableview,Uilabel,从推送导航返回后,我正在尝试更新自定义单元格上某个标签的分数 在我的家长UITableViewController中,我有以下代码: 在视图中加载 //These are mutable strings self.disciplineScoreString1 = [NSMutableString stringWithFormat:@""]; self.disciplineScoreString2 = [NSMutableString stringWithFormat:@""]; self.dis
UITableViewController
中,我有以下代码:
在视图中加载
//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];
到目前为止还不错
在单元格中,按行索引路径
//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
,并从子UIViewController
返回,成功地将self.practicescorrestring1
的字符串值设置为@“10”
,并记录在父UITableViewController
中
现在如何更新第一个单元格的标签?我已尝试在视图中重新加载数据
,但它不起作用
谢谢
编辑
这是子视图控制器中的代码
在视图中将消失:
[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
第二个示例是错误的,因为您正在创建一个新字符串,而数组中的对象仍然指向旧的原始字符串
编辑:
你目前的方法很僵化,不太标准。您通常会设置某种委托,将数据从子级传递到父级。孩子应该知道如何触及父母的内脏并改变其数据,这一点很少是正确的
我个人会从这样的事情开始
@interface ChildViewController : UIViewController
@property (nonatomic, copy) void (^onCompletion)(ChildViewController *viewController, NSString *disciplineText);
@end
现在,我假设您通过在子系统中调用[self.navigationController popViewControllerAnimated:YES]
来解除此任务?这有点奇怪,因为这意味着childViewController现在只能在navigationController中显示,这使得它的可重用性降低
我要做的是在您通常调用popViewControllerAnimated:
的时候,我会调用完成
if (self.onCompletion) {
self.onCompletion(self, self.disciplineText);
}
现在,提供onCompletion
的对象可以决定如何删除此控制器,并告诉它我们已完成的数据,这使控制器能够对其执行它想要的操作
在您的情况下,父控制器将提供完成,因为它知道如何显示子控制器,所以它将知道如何取消它。它还知道它可能想对孩子完成的数据做些什么
// Where you present the child
childViewController.disciplineText = self.self.disciplineScoreArray[index];
__weak __typeof(self) weakSelf = self;
childViewController.onCompletion = ^(ChildViewController *viewController, NSString *disciplineText){
weakSelf.disciplineScoreArray replaceObjectAtIndex:index withObject:disciplineText];
[self.navigationController popViewControllerAnimated:YES];
[weakSelf.tableView reloadRowsAtIndexPaths:@[ [NSIndexPath indexPathForRow:index inSection:0] ]
withRowAnimation:UITableViewRowAnimationFade];
};
你能显示你设置值的代码吗?看起来@merlevede的答案是正确的<代码>视图将消失:
可能不是放置此逻辑的最佳位置。这通常通过委托或某种类型的完成处理程序来完成。按照您现有的方式进行操作会使您的代码非常有用rigid@Paul.s你能给我一个替代方案吗?我接受你的回答。我已经编辑了问题,并添加了我试图更改字符串的位置。我有点明白你的意思,但是你能帮我进一步修改一下我刚才做的编辑吗谢谢你的款待。我还应该向其他读者添加,您应该在视图中调用[self.tableView reload data]将出现在父TVCJ上,以便进一步澄清。如果纪律森林1是一个NSString,那么我的情况根本不起作用?
// Where you present the child
childViewController.disciplineText = self.self.disciplineScoreArray[index];
__weak __typeof(self) weakSelf = self;
childViewController.onCompletion = ^(ChildViewController *viewController, NSString *disciplineText){
weakSelf.disciplineScoreArray replaceObjectAtIndex:index withObject:disciplineText];
[self.navigationController popViewControllerAnimated:YES];
[weakSelf.tableView reloadRowsAtIndexPaths:@[ [NSIndexPath indexPathForRow:index inSection:0] ]
withRowAnimation:UITableViewRowAnimationFade];
};