0

我有一个tableviewcellRegisterViewController当我点击它时,它会推送到SelectCityViewController我可以从中选择一个城市的地方uipickerview。我已经在SelectCityViewController. 但是当我实现委托方法时,我从 中取回了城市并从中pickerview选择indexpath了。当我检查日志时,我得到了城市和选定的. 但是当我打电话时,单元格仍然有旧的标题标签文本。编码:didSelectRowAtIndexPathRegisterViewControllerindexpathreloadRowsAtIndexPaths

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

2 回答 2

1

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

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

于 2013-03-26T06:45:05.853 回答
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;
}
于 2013-03-26T06:32:26.933 回答