我有一个具有可变行数(单元格)的 UITableView - 这些行的高度是恒定的。我想要的是使 UITableView 的高度取决于行数。
另外,我希望 UITableView 能够完全包裹单元格,所以底部没有填充。因此,如果一个单元格的高度为 60,则一个单元格的 tableview 应该是 60,两个单元格应该是 120,等等......
提前致谢!
我有一个具有可变行数(单元格)的 UITableView - 这些行的高度是恒定的。我想要的是使 UITableView 的高度取决于行数。
另外,我希望 UITableView 能够完全包裹单元格,所以底部没有填充。因此,如果一个单元格的高度为 60,则一个单元格的 tableview 应该是 60,两个单元格应该是 120,等等......
提前致谢!
您可以访问您的数据源以查找将显示的单元格数量。将此数字乘以一行的高度,即可得到整个表格视图的高度。要更改表格视图的高度,您可以更改其 frame 属性。
rowHeight
您可以通过访问其属性来访问表格视图的一行的(恒定)高度。如果使用名为 的数组的对象填充表视图,则myArray
可以使用以下代码:
CGFloat height = self.tableView.rowHeight;
height *= myArray.count;
CGRect tableFrame = self.tableView.frame;
tableFrame.size.height = height;
self.tableView.frame = tableFrame;
您还可以通过询问表格视图本身而不是数据对象来了解表格视图将包含多少行。像这样的东西应该工作:
NSInteger numberOfCells = 0;
//finding the number of cells in your table view by looping through its sections
for (NSInteger section = 0; section < [self numberOfSectionsInTableView:self.tableView]; section++)
numberOfCells += [self tableView:self.tableView numberOfRowsInSection:section];
CGFloat height = numberOfCells * self.tableView.rowHeight;
CGRect tableFrame = self.tableView.frame;
tableFrame.size.height = height;
self.tableView.frame = tableFrame;
//then the tableView must be redrawn
[self.tableView setNeedsDisplay];
我就是这样解决的。
CGFloat height = self.mMyTicketTable.rowHeight;
float h = height * [self.mArrEName count];
if(h > 410.0)
{
self.mMyTicketTable.frame = CGRectMake(0, 50, self.mMyTicketTable.frame.size.width, 410);
}
else
{
self.mMyTicketTable.frame = CGRectMake(0, 50, self.mMyTicketTable.frame.size.width, h);
}
NSLog(@"h:%f", h);
实现这个 UITableView 委托方法。每当创建一个单元格行时,它都会为每个单元格分配高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60.0f;
}
并在 viewDidLoad 方法中实现此代码
[yourTableView setFrame:CGRectMake(yourTableView.frame.origin.x, yourTableView.frame.origin.y, yourTableView.frame.size.width,(60.0f*([yourArray count])))];
注意:- yourArray 是一个 NSArray,其中包含要创建多少行的数据。
或者
如果 yourArray 动态获取值,则在获取值后调用此方法。
-(void)setHeightOfTableView
{
/**** set frame size of tableview according to number of cells ****/
float height=60.0f*[yourArray count];
[yourTableView setFrame:CGRectMake(yourTableView.frame.origin.x, yourTableView.frame.origin.y, yourTableView.frame.size.width,(60.0f*([yourArray count])))];
}
我希望它有效。谢谢
我根据@timvermeulen 的代码创建了一个函数来设置 uitableview 高度
-(void)setTableViewheightOfTable :(UITableView *)tableView ByArrayName:(NSArray *)array
{
CGFloat height = tableView.rowHeight;
height *= array.count;
CGRect tableFrame = tableView.frame;
tableFrame.size.height = height;
tableView.frame = tableFrame;
}
您可以使用委托方法tableView:numberOfRowsInSection:
动态设置 UITableView 高度,如下所示:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
CGFloat numberOfRows = self.myArray.count;
CGRect tableViewFrame = tableView.frame;
tableViewFrame.size.height = numberOfRows * tableView.rowHeight;
tableView.frame = tableViewFrame;
return numberOfRows;
}
还要记住定义rowHeight
启动表视图的位置:
self.myTableView.rowHeight = 60.0;
...但是如果您的目标只是隐藏表格中的空单元格,则应向其添加一个空视图tableFooterView
并忽略上面的所有代码(因此请先尝试此行):
self.myTableView.tableFooterView = [UIView new];