我在tableviewcell
中有一个RegisterViewController
,当我单击它时,它会推到SelectCityViewController
,在这里我可以从uipickerview
中选择城市。我已经在SelectCityViewController
中定义了一个委托。但是,当我实现委托方法时,我从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;
}