ios - 如何更改不同部分中的uitableviewcell数据?

标签 ios uitableview

我想在选择其中一个部分时更改两个不同部分中两行的数据,以便它们具有相同的答案。但它不起作用:

   NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:row1 inSection:section1];
    cell = [self.tableView cellForRowAtIndexPath:indexPath1];
    //DO SOMETHING WITH CELL LABEL


    NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:row2 inSection:section2];
    cell = [self.tableView cellForRowAtIndexPath:indexPath2];
    //MAKE THIS CELL LABEL SAME AS THE LAST ONE

如果第二个单元格位于不同的部分,则不起作用!有什么建议吗?

最佳答案

如果其中一个单元格当前不可见,您将从 cellForRowAtIndexPath: 得到 nil。 因此,您应该从数据模型而不是 UI 中获取所需的条目。 在那里更改所需的信息:

NSArray *section1Data = [self.dataModell objectAtIndex:section1];
NSDictionary *dataObject1 = [section1Data objectAtIndex:row1];
[dataObject1 setValue:@"My new Text." forKey:@"theTextPropertyKey"];

NSArray *section2Data = [self.dataModell objectAtIndex:section2];
NSDictionary *dataObject2 = [section1Data objectAtIndex:row2];
[dataObject2 setValue:@"My new Text." forKey:@"theTextPropertyKey"];

然后在 UI 中重新加载所需的单元格:

NSArray *indexPaths = @[indexPath1, indexPath2];
[self.tableView reloadRowsAtIndexPaths:indexPaths];

关于ios - 如何更改不同部分中的uitableviewcell数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13181555/

相关文章:

ios - 找不到 Cocos2D kazmath/kazmath.h 文件

iphone - 创建自定义动画属性

ios - UIDocumentInteractionController 注解属性使用

swift - NSIndexPath 永远不能为 nil,不允许比较

ios - 如何解决位置:fixed for a button at the bottom of the screen in iOS (ipad/iphone)?的问题

ios - 从服务器下载@2x 和@3x 图像到 Tableview

ios - 如何使用 UINib 实例化和使用自定义 UITableViewCells

ios - UITableView 决定单个行高

swift - Tableview 数据不会使用 NSUserDefaults 打印

ios - Cocos2d 缩放 Sprite 会导致伪影