我有一个 tableviewcell
在RegisterViewController
,当我点击它时,它会推送到 SelectCityViewController
我可以从 uipickerview
中选择一个城市.我在 SelectCityViewController
中定义了一个委托(delegate).但是当我实现委托(delegate)方法时,我从 pickerview
得到了城市。和选定的 indexpath
来自 didSelectRowAtIndexPath
在RegisterViewController
.当我检查日志时,我得到了城市和选定的 indexpath
.但是当我调用reloadRowsAtIndexPaths
,单元格仍然有旧的 titlelabel 文本。代码:
SelectCityViewController.h
@protocol SelectCityDelegate <NSObject>
- (void)didGetCity:(City *)city;
@end
SelectCityViewController.m
- (void)finished:(UIBarButtonItem *)buttonItem
{
if ([self.delegate respondsToSelector:@selector(didGetCity:)]) {
[self.delegate didGetCity:self.city];
}
NSLog(@"%@", self.city.city);
[self.navigationController popViewControllerAnimated:YES];
}
RegisterViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 2) {
self.selectedIndexPath = indexPath;
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
SelectCityViewController *signUp = [storyBoard instantiateViewControllerWithIdentifier:@"SignVC"];
signUp.delegate = self;
[self.navigationController pushViewController:signUp animated:YES];
}
}
- (void)didGetCity:(City *)city
{
NSLog(@"%@", city.city);
NSLog(@"%@", selectedIndexPath);
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:selectedIndexPath];
cell.textLabel.text = city.city;
[self.tableView reloadRowsAtIndexPaths:@[selectedIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
最佳答案
我认为问题出在下面的代码中,您不必在此处设置值,因为当您重新加载时,它将使用您创建单元格的函数中的值覆盖该值。
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:selectedIndexPath];
cell.textLabel.text = city.city;
只需重新加载单元格,并将上面的代码放入您的
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Cell intialization and other things
cell.textLabel.text = city.city;
}
关于iphone - uitableview reloadRowsAtIndexPaths 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15630752/