美文网首页
(Objective-C) 精准计算UITableView.co

(Objective-C) 精准计算UITableView.co

作者: 布呐呐u | 来源:发表于2025-08-02 00:34 被阅读0次
- (int)calculateExactTableViewHeight {
    [self.tableView reloadData];
    [self.tableView layoutIfNeeded];
    
    CGFloat totalHeight = 0;
    NSInteger sectionCount = self.tableView.numberOfSections;
    
    for (NSInteger section = 0; section < sectionCount; section++) {
        NSInteger rowCount = [self.tableView numberOfRowsInSection:section];
        
        for (NSInteger row = 0; row < rowCount; row++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
            CGRect rowRect = [self.tableView rectForRowAtIndexPath:indexPath];
            totalHeight += CGRectGetHeight(rowRect);
        }
        
        CGRect headerRect = [self.tableView rectForHeaderInSection:section];
        CGRect footerRect = [self.tableView rectForFooterInSection:section];
        totalHeight += CGRectGetHeight(headerRect) + CGRectGetHeight(footerRect);
    }
    
    return ceilf(totalHeight);
}

相关文章

网友评论

      本文标题:(Objective-C) 精准计算UITableView.co

      本文链接:https://www.haomeiwen.com/subject/jgqhojtx.html