Jerky Scrolling After Updating UITableViewCell in place with UITableViewAutomaticDimension

前端 未结 6 560
我在风中等你
我在风中等你 2020-12-04 05:49

I am building an app that has a feed view for user-submitted posts. This view has a UITableView with a custom UITableViewCell implementation. Insid

6条回答
  •  执笔经年
    2020-12-04 06:20

    We had the same problem. It comes from a bad estimation of the cell height that causes the SDK to force a bad height which will cause the jumping of cells when scrolling back up. Depending on how you built your cell, the best way to fix this is to implement the UITableViewDelegate method - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath

    As long as your estimation is pretty close to the real value of the cell height, this will almost cancel the jumping and jerkiness. Here's how we implemented it, you'll get the logic:

    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
        // This method will get your cell identifier based on your data
        NSString *cellType = [self reuseIdentifierForIndexPath:indexPath];
    
        if ([cellType isEqualToString:kFirstCellIdentifier])
            return kFirstCellHeight;
        else if ([cellType isEqualToString:kSecondCellIdentifier])
            return kSecondCellHeight;
        else if ([cellType isEqualToString:kThirdCellIdentifier])
            return kThirdCellHeight;
        else {
            return UITableViewAutomaticDimension;
        }
    }
    

    Added Swift 2 support

    func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        // This method will get your cell identifier based on your data
        let cellType = reuseIdentifierForIndexPath(indexPath)
    
        if cellType == kFirstCellIdentifier 
            return kFirstCellHeight
        else if cellType == kSecondCellIdentifier
            return kSecondCellHeight
        else if cellType == kThirdCellIdentifier
            return kThirdCellHeight
        else
            return UITableViewAutomaticDimension  
    }
    

提交回复
热议问题