ios - 设置 UITableView 的框架以匹配其内容大小

标签 ios uitableview uiscrollview

我在较大的 UIScrollView 中有一个非滚动 UITableView。由于表格 View 是非滚动的,我希望它的框架大小总是匹配它的内容大小。

我有以下方法:

- (void)resizeTableViewFrameHeight
{
    // Table view does not scroll, so its frame height should be equal to its contentSize height
    CGRect frame = self.tableView.frame;
    frame.size = self.tableView.contentSize;
    self.tableView.frame = frame;
}

问题是我必须在错误的时间调用此方法(在设置 contentSize 之前)。我想知道,我是否可以在 UITableView 上设置一个属性,使其框架始终自动匹配其内容大小?

最佳答案

没有动态调整表格 View 框架的属性。另外,我不认为 UITableView可以计算其整个contentSize预先设置,因为表格 View 的大小实际上可能是无限的,这取决于它的 datasource 中指定的内容。和其他委托(delegate)方法,例如 heightForRowAtIndexPath .这些方法可以是动态的,因此几乎不可能预测 table view 的整个大小。

我得出的结论是,您可能没有正确使用此 UI 元素。

解决方法:

通过将表格 View 的框架限制在 ScrollView 的可见区域,您可以简单地允许它滚动。也许您需要防止 ScrollView 在表格 View 滚动时滚动,反之亦然。您可以在 UIScrollView 中轻松做到这一点委托(delegate)方法。

关于ios - 设置 UITableView 的框架以匹配其内容大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9956291/

相关文章:

ios - 由于丢失私有(private)钥匙串(keychain)而吊销证书的后果?

swift - TableView 类别细化

ios - UITableViewCell 子类 : in what delegate method is it appropriate to do changes to the cells layer?

iphone - 缩放后重新定位 UIButtons

ios - 如果我有不同的 UI 控件要顺序显示,我是否仍然应该使用 TableView Controller ?

ios - 为标签的相同字符设置不同的颜色

ios - "Swipe to go back"手势在 WatchOS 中无法正常工作

ios - 在 UIScrollView 中允许单击手势识别器和双击

iphone - 有没有办法跟踪 App Store 访问者?

ios - Swift 3 : Segmented Control into TableView. 重新加载表时所选索引丢失