UITableView bouncing back to the top of a section when calling reloadRowsAtIndexPaths

后端 未结 9 1900
既然无缘
既然无缘 2020-12-15 03:14

When a user taps a button in one of my rows I am updating the underlying model for that row and then calling reloadRowsAtIndexPaths for the given row (i.e. single row reload

相关标签:
9条回答
  • 2020-12-15 03:51

    In my similar case, I had to tweak the implementation of method

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ...
    }
    

    where my heights were being reset. So, instead of resetting height for every cell I updated only the effected cells for reloadRowsAtIndexPaths call.

    0 讨论(0)
  • 2020-12-15 03:52

    If you know the minimum height of your cell, you must specify that while setting estimatedRowHeight. I was setting it to 1 as I have read before somewhere that any value above 0 would suffice the purpose, but it was the culprit.

    When I set it to 44, which was the minimum height my cell could have, all went fine.

    Dynamic heights were also working fine, no issues with this fix.

    0 讨论(0)
  • 2020-12-15 03:52

    To build off xsee's answer - I had set the Estimate in interface builder to "automatic". I changed this to another number and it started working. I kept Row Height to automatic.

    0 讨论(0)
  • 2020-12-15 04:05

    You should be able to do what you are trying to do by changing the cell contents directly. For example, if you are using the base UITableViewCell class and the data in your model is a NSString which you show in the table view cell, you can do the following (after you change your data model) instead of calling reloadRowsAtIndexPaths:

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UILabel *label = [cell textLabel];
    label.text = @"New Value";
    

    If you are using a custom subclass of UITableViewCell, it's roughly the same except for accessing the views of the cell will need to be done through the contentView property.

    0 讨论(0)
  • 2020-12-15 04:10

    This did the trick for me.

    UIView.setAnimationsEnabled(false)
    tableView.reloadRows(at: [...], with: .none)
    UIView.setAnimationsEnabled(true)
    
    0 讨论(0)
  • 2020-12-15 04:10

    I had the same issue. I ended up just calling tableView.reloadData() instead after updating my data / cell and it didn't bounce back to the top / data was updated in place - FYI

    0 讨论(0)
提交回复
热议问题