我想在选择其中一行时更改两个不同部分中两行的数据,以便它们具有相同的答案。但它不起作用:
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
如果第二个单元格位于不同的部分,则无效!有什么建议吗?
答案 0 :(得分:1)
如果其中一个单元格此刻不可见,您将从 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];