minjiera
minjiera

Reputation: 336

How to change uitableviewcell data in different section?

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. But it is not working:

   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

It doesn't work if the second cell is in a different section! Any suggestion?

Upvotes: 0

Views: 180

Answers (1)

Thyraz
Thyraz

Reputation: 2432

You will get a nil from cellForRowAtIndexPath: if one of the cells isn't visible at the moment. You should therefore get the needed entry from your data-model, not from the UI. Change the needed information there:

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"];

Then reload the needed cells in the UI:

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

Upvotes: 1

Related Questions