UITableview Scrolls to Top on Reload

后端 未结 5 1304
不知归路
不知归路 2020-12-28 18:23

I am facing problem in my app - you can post and edit your favorite places. After posting a post, or editing a specific post (UITableViewCell), UITablevie

5条回答
  •  长发绾君心
    2020-12-28 18:45

    To prevent scrolling to top, you should save heights of cells when they loads and give exact value in tableView:estimatedHeightForRowAtIndexPath:

    // declare cellHeightsDictionary
    NSMutableDictionary *cellHeightsDictionary;
    
    // initialize it in ViewDidLoad or other place
    cellHeightsDictionary = @{}.mutableCopy;
    
    // save height
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        [cellHeightsDictionary setObject:@(cell.frame.size.height) forKey:indexPath];
    }
    
    // give exact height value
    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
        NSNumber *height = [cellHeightsDictionary objectForKey:indexPath];
        if (height) return height.doubleValue;
        return UITableViewAutomaticDimension;
    }
    

提交回复
热议问题