2013-03-26 98 views
0

我在RegisterViewController中有tableviewcell,当我点击它时,它会推送到SelectCityViewController,我可以从uipickerview中选择一个城市。我已经在SelectCityViewController中定义了一位代表。但是,当我实施代表方法时,我从pickerviewindexpathdidSelectRowAtIndexPathRegisterViewController得到了城市。当我检查日志时,我得到了城市,并选择了indexpath。但是当我打电话给reloadRowsAtIndexPaths时,单元格仍然有旧的titlelabel文本。验证码:uitableview 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]; 
} 
+0

有什么错误?如果在重新加载tableview之后发生错误,那么使用错误日志编辑您的问题。 – Tirth 2013-03-26 06:35:13

+0

没有错误。 titlelabel.text不会改变。 – 2013-03-26 09:16:43

回答

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

@ [selectedIndexPath]是一个数组。它与您所写的内容完全相同,只是使用新的数组文字语法。 – rdelmar 2013-03-26 06:38:48

+0

感谢您的指点,我错过了看到:-p – iphonic 2013-03-26 06:44:21

+0

但是当应用程序开始运行时,我已经将单元格的文本设置为cell.textLabel.text = @“选择一个城市”;当我点击这个单元格时,它应该推一个视图控制器,我可以从pickerview中选择一个城市,然后返回到这个单元格,我希望这个单元格的文本更改为city.city。 – 2013-03-26 09:22:02

1

didGetCity需要更新备份的表,而不是表细胞本身的模型。你如何回答numberOfRowsInSection:?随着一些数组的计数?

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

+0

城市单元格是indexpath.section = 2中的一个静态单元格。 – 2013-03-26 09:16:09

+0

numberOfRowsInSection刚刚为本节回退1。2.在第1节中,我得到了另一个数组支持表格。 – 2013-03-27 00:51:44