ios 基于单元格数量的 UITableView 高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13001944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITableView height based on number of cells
提问by Christian-G
I have a UITableView with a variable number of rows (cells) - the height of these rows is constant. What I would like is to make the height of the UITableView depend on the number of rows.
我有一个带有可变行数(单元格)的 UITableView - 这些行的高度是恒定的。我想要的是使 UITableView 的高度取决于行数。
Also, I would like the UITableView to be exactly wrapping the cells, so no padding at the bottom. So if a cell has height 60, tableview should be 60 for one cell, 120 for two cells, etc...
另外,我希望 UITableView 完全包裹单元格,因此底部没有填充。因此,如果单元格的高度为 60,则一个单元格的 tableview 应为 60,两个单元格的高度应为 120,等等......
Thanks in advance!
提前致谢!
回答by Tim Vermeulen
You could access your data source to find the number of cells that will be displayed. Multiply this number by the height of one row and you get the height of the entire table view. To change the height of a table view, you can change its frame property.
您可以访问数据源以查找将显示的单元格数量。将此数字乘以一行的高度,您将得到整个表格视图的高度。要更改 table view 的高度,您可以更改它的 frame 属性。
You can access the (constant) height of one row of your table view by accessing its rowHeight
property. If you fill your table view with the objects of an array called myArray
, you could use the following code:
您可以通过访问其rowHeight
属性来访问表格视图一行的(恒定)高度。如果myArray
使用名为 的数组的对象填充表视图,则可以使用以下代码:
CGFloat height = self.tableView.rowHeight;
height *= myArray.count;
CGRect tableFrame = self.tableView.frame;
tableFrame.size.height = height;
self.tableView.frame = tableFrame;
You could also find out how many rows the table view will contain by asking the table view itself, instead of the data object. Something like this should work:
您还可以通过询问表视图本身而不是数据对象来找出表视图将包含多少行。这样的事情应该工作:
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];
回答by Christian-G
This is how i solved.
我就是这样解决的。
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);
回答by chandan
Implement this UITableView delegate method . Whenever a cell row create it assign height to each cell
实现这个 UITableView 委托方法。每当单元格行创建时,它都会为每个单元格分配高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60.0f;
}
and implement this code in viewDidLoad method
并在 viewDidLoad 方法中实现此代码
[yourTableView setFrame:CGRectMake(yourTableView.frame.origin.x, yourTableView.frame.origin.y, yourTableView.frame.size.width,(60.0f*([yourArray count])))];
Note :-yourArray is an NSArray which contain data that how many row you want to crate.
注意:-yourArray 是一个 NSArray,其中包含要创建多少行的数据。
OR
或者
If yourArray fetching value dynamically then call this method after fetching value.
如果 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])))];
}
I hope it works. Thanks
我希望它有效。谢谢
回答by g212gs
I made a function to set the uitableview height as per @timvermeulen's code
我做了一个函数来根据@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;
}
回答by turingtested
You could use the delegate method tableView:numberOfRowsInSection:
to set the UITableView height dynamically, like this:
您可以使用委托方法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;
}
Remember also to define the rowHeight
where you initiate the table view:
还要记住定义rowHeight
启动表视图的位置:
self.myTableView.rowHeight = 60.0;
... but if your goal is just to hide empty cells in the table, you should add an empty view to it's tableFooterView
instead and ignore all the code above (so try this line first):
...但如果您的目标只是隐藏表格中的空单元格,您应该向其添加一个空视图tableFooterView
并忽略上面的所有代码(因此请先尝试这一行):
self.myTableView.tableFooterView = [UIView new];