0

I want to change the data of two rows in two different sections when one of them is selected so that they will have the same answer. 但它不起作用:

   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

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

4

1 回答 1

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];
于 2012-11-01T16:51:25.860 回答