UITableView Refresh without scrolling

2020-05-25 03:49发布

I have a _TableView with items , and I want to set automatic refresh,and I don't want it to scroll on refresh , lets say user scrolled 2 pages down , and the refresh trigered -> so I want to put the refreshed content to the top of the table without interupting user's scrolling

Assume user was on row 18 and now the _dataSource is refreshed so it fetched lets say 4 items , so I want user to stay on the item he was.

What would be the best approach to achieve it ??

11条回答
▲ chillily
2楼-- · 2020-05-25 03:59

try to replace

reloadData with

tableView.reloadRows(at: tableView!.indexPathsForVisibleRows!, with: .none),

but you should be care about no cells, if no cells, this method should cause crash.

查看更多
Explosion°爆炸
3楼-- · 2020-05-25 04:00

When you want to reload you have to

self.tableView.reloadData()

self.tableView.layoutIfNeeded()

and also use this UITableViewDelegate

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 'your maximum cell's height'
}

and your tableView will remain on the previous scroll position without scrolling

查看更多
老娘就宠你
4楼-- · 2020-05-25 04:01

I am showing if only one row is being added. You can extend it to multiple rows.

    // dataArray is your data Source object
    [dataArray insertObject:name atIndex:0];
    CGPoint contentOffset = self.tableView.contentOffset;
    contentOffset.y += [self tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    [self.tableView reloadData];
    [self.tableView setContentOffset:contentOffset];

But for this to work you need to have defined - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath the method. Or else, you can directly give your tableview row height if it is constant.

查看更多
萌系小妹纸
5楼-- · 2020-05-25 04:01

For Swift 3+:

You need to save the current offset of the UITableView, then reload and then set the offset back on the UITableView.

I have created this function for this purpose:

func reload(tableView: UITableView) {

    let contentOffset = tableView.contentOffset
    tableView.reloadData()
    tableView.layoutIfNeeded()
    tableView.setContentOffset(contentOffset, animated: false)

}

Simply call it with: reload(tableView: self.tableView)

查看更多
贼婆χ
6楼-- · 2020-05-25 04:03

I'm doing it this way:

messages.insertContentsOf(incomingMsgs.reverse(), at: 0)
table.reloadData()

// This is for the first load, first 20 messages, scroll to bottom
if (messages.count <= 20) {
      let indexToScroll = NSIndexPath(forRow: saferSelf.messages.count - 1, inSection: 0)
      table.scrollToRowAtIndexPath(indexToScroll, atScrollPosition: .Top , animated: false)
}
// This is to reload older messages on top of tableview
else {
      let indexToScroll = NSIndexPath(forRow: incomingMsgs.count, inSection: 0)
      table.scrollToRowAtIndexPath(indexToScroll, atScrollPosition: .Top , animated: false)
      // Remove the refreshControl.height + tableHeader.height from the offset so the content remain where it was before reload
      let theRightOffset = CGPointMake(0, table.contentOffset.y - refreshControl.frame.height - table.headeView.frame.height)
      table.setContentOffset(theRightOffset, animated: false)
}

...also, since I use dynamic cell height, to avoid some weirdness, the estimation is cached:

var heightAtIndexPath = [NSIndexPath: CGFloat]()
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return heightAtIndexPath[indexPath] ?? UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    heightAtIndexPath[indexPath] = cell.frame.height
}
查看更多
再贱就再见
7楼-- · 2020-05-25 04:04

Just set estimatedRowHeight to maximum possible value.

 self.tableView.estimatedRowHeight = 1000
 self.tableView.estimatedSectionFooterHeight = 100.0
 self.tableView.estimatedSectionHeaderHeight = 500.0

That's it!!

Note:

Please do not use FLT_MAX, DBL_MAX value. May be it will crash your app.

查看更多
登录 后发表回答