ios - 设置tableview的动态高度

标签 ios iphone uitableview ios7

我的屏幕上有一个表格 View 。我只想以最大高度显示该 TableView ,但我想根据行设置它的高度。如果有两行,那么它的高度应该最多为两行。不应显示 TableView 的其他部分。

enter image description here

您现在可以看到只有两行,但表格高度更高,所以它显示了那部分。我想动态设置表格高度。我不知道如何设置。我也用谷歌搜索,但虽然我没有找到任何解决方案

最佳答案

我是这样做的: 高度 = 屏幕高度 - 导航栏高度的 UITableView。 即

height = self.view.frame.size.height-64

self.dataArrayForCategory is our Datasource and 44 is our RowHeight.
[self.tblHomeMenu setFrame:CGRectMake(0, 0, 320,
44*self.dataArrayForCategory.count)];
(self.tblHomeMenu.frame.size.height>height)?[self.tblHomeMenu
setFrame:CGRectMake(0, 0, 320,
height)]:[self.tblHomeMenu
setFrame:CGRectMake(0, 0, 320, 44*self.dataArrayForCategory.count)];
if (self.tblHomeMenu.frame.size.height>self.view.frame.size.height-64)
{
    [self.tblHomeMenu setScrollEnabled:YES];
}

我有一个高度为 Screensize - Navigationbar 的 UITableView。

如果 UITableView 高度 > Screensize - Navigationbar 则使用最后一行,那么 UITableView 应该是可滚动的,否则它不应该。

在您的情况下,您的高度将是您的 UITableView 的高度。

关于ios - 设置tableview的动态高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21017052/

相关文章:

ios - 新鲜的App Store提交与应用程序更新

iphone - iOS 表单控件

ios - ImageView 没有回到合适的状态

ios - 为什么这个简单的不透明动画在 SwiftUI 中不起作用?

ios - 如何禁用 NSMutableAttributedString 中标签的下划线 url

objective-c - 没有 UIImageView 的 UIImage 的反射

iphone - 为什么我的函数被调用了两次?

ios - 检查 iOS9 设备是否支持并启用 3D touch

ios - 导航后自定义 UITableViewCell 未释放或重用,内存泄漏

iphone - 为什么调用 insertRowsAtIndexPaths : call heightForRow for all rows?