We can insert cells to UITableView or UICollectionView and if the cells will be inserted to middle or bottom, we will feel smooth and stay at current offset. But when I insert at top, tableview or UICollectionView will move to Offset zero automatically, means I will not stay at current visible cells I want. How can I implement it, is it native feature or do we have to make some complex code? I see the FB messenger or Skype app, when scroll to top, new messages will be inserted without any change of current visible view. Anyone suggest to me a solution for this?
If you create a new simple project using master-Detail application you can see when you insert it doesn't scroll to top the content offset is only changed a bit (equal to new row height) even if you are in middle ,top or bottom it is inserting at the top . . it using simple code like
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
Now even if you don't want that little scrolling here is nice solution
//just need check if you already on the top of the tableview that may be first few rows depend on your row height
if(self.tableView.contentOffset.y > ONE_OR_X_ROWS_HEIGHT_YOU_DECIDE
{
self.delayOffset = self.tableView.contentOffset;
self.delayOffset = CGPointMake(self.delayOffset.x, self.delayOffset.y+ insertedRowCount * ONE_ROW_HEIGHT);
//insert your row here
[self.tableView setContentOffset:self.delayOffset animated:NO];
}else
{
//insert your row here
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.