簡體   English   中英

uitableview reloadRowsAtIndexPaths無效

[英]uitableview reloadRowsAtIndexPaths not working

我在RegisterViewController有一個tableviewcell ,當我點擊它時,它會推送到SelectCityViewController ,我可以從uipickerview選擇一個城市。 我在SelectCityViewController定義了一個委托。 但是當我實現委托方法時,我從pickerview了城市,並從RegisterViewController didSelectRowAtIndexPath中選擇了indexpath 當我檢查日志時,我得到了城市和選定的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];
}

我認為問題在於下面的代碼,你不必在這里設置值,因為當你重新加載時,它將覆蓋你創建單元格的函數中的值。

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

didGetCity需要更新支持表的模型,而不是表格單元本身。 你如何回答numberOfRowsInSection: 隨着一些數組的計數?

索引selectedIndexPath.row那個數組需要改變。 cellForRowAtIndexPath:應使用相同的數據初始化單元格。 然后你的didGetCity方法更新數組並重新加載。 它不應該指細胞。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM