uitableview reloadRowsAtIndexPaths无效

时间:2013-03-26 06:30:26

标签: iphone ios delegates tableview

我在tableviewcell中有一个RegisterViewController,当我点击它时,它会推送到SelectCityViewController我可以从uipickerview中选择一个城市。我在SelectCityViewController中定义了一个委托。但是当我实现委托方法时,我从pickerview中取回了城市,并从indexpath中的didSelectRowAtIndexPath中选择了RegisterViewController。当我查看日志时,我会看到城市和所选的indexpath。但是当我打电话给reloadRowsAtIndexPaths时,单元格仍然有旧的标题文本。代码:

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];
}

2 个答案:

答案 0 :(得分:1)

我认为问题在于下面的代码,你不必在这里设置值,因为当你重新加载时,它将覆盖你创建单元格的函数中的值。

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;
}

答案 1 :(得分:1)

didGetCity需要更新支持表的模型,而不是表格单元本身。你如何回答numberOfRowsInSection:?有一些数组的计数?

索引selectedIndexPath.row处的数组需要更改。 cellForRowAtIndexPath:应使用相同的数据初始化单元格。然后你的didGetCity方法更新数组并重新加载。它不应该指细胞。