Reloading custom UITableViewCell after reordering cells

后端 未结 6 1630
忘了有多久
忘了有多久 2021-02-15 16:04

I have UITableView which uses custom UITableViewCells. The cells can have one of three types of background images (set in each cell\'s .backgrou

6条回答
  •  轮回少年
    2021-02-15 16:41

    (this solution only works for the reordering of rows within the same section. It'll fail when dragging and dropping rows to a different section)

    I had the same problem when updating a cell's detail label text after being reordered.

    I solved it with the following code:

    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath  toIndexPath:(NSIndexPath *)toIndexPath 
    {
    UITableViewCell* cell = [self.table cellForRowAtIndexPath:fromIndexPath]; 
    cell.detailTextLabel.text = @"NEW STRING";
    }
    

    Note that you have to update the cell at fromIndexPath, because it seems that the internal state of the tableview doesn't update until this method returns. If you get the cell at toIndexPath, after reordering finishes, you will see the neighbor cell gets updated. (either top or bottom one).

提交回复
热议问题