UITableView高度基于单元格数

时间:2012-10-21 20:36:22

标签: objective-c ios uitableview

我有一个带有可变行数(单元格)的UITableView - 这些行的高度是常量。我想要的是使UITableView的高度取决于行数。

另外,我希望UITableView正好包裹单元格,所以底部没有填充。因此,如果一个单元格的高度为60,那么tableview对于一个单元格应为60,对于两个单元格应为120,等等...

提前致谢!

5 个答案:

答案 0 :(得分:28)

您可以访问数据源以查找将要显示的单元格数。将此数字乘以一行的高度,您将获得整个表格视图的高度。要更改表视图的高度,可以更改其框架属性。

您可以通过访问其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];

答案 1 :(得分:2)

这就是我解决的问题。

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);

答案 2 :(得分:2)

实现此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,其中包含您想要创建多少行的数据。

OR

如果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])))];
}

我希望它有效。感谢

答案 3 :(得分:2)

我根据@ 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;
}

答案 4 :(得分:0)

您可以使用委托方法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];
相关问题