Resize UITableView's height to be as high as i

2019-02-02 00:52发布

So I am having a very basic issue. I have this UITableView within a view, and I would like to make this table's height as high as it needs to be to display ALL the rows within the table. Therefore, I don't want to be able to scroll, I just want all the rows to appear. (The rows are dynamic in quantity and height).

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize cellSize = [[[_stepsArray objectAtIndex:indexPath.row]content] sizeWithFont:[UIFont boldSystemFontOfSize:18.0f] constrainedToSize:CGSizeMake(528.0f, CGFLOAT_MAX)lineBreakMode:NSLineBreakByWordWrapping];
    return cellSize.height;
}

If there are 20 rows, the table will be very high, but if there is only 1 row, it will be very small and the other content will not appear 19 rows below.

6条回答
Root(大扎)
2楼-- · 2019-02-02 01:42

Simply use tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; That will solve your problem.

查看更多
劫难
3楼-- · 2019-02-02 01:45

Here is more simpler solution: All you need is to set sizeToFit on your UITableView, after it has been populated with cells. If you set delegate and dataSource through xib, you can do this in your viewDidLoad method, or viewDidAppear, like this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [yourTableView sizeToFit];
}

But, if you add delegate and dataSource somewhere in the code, you have to put sizeToFit after this:

- (void)someMethod
{
    [yourTableView setDelegate:self];
    [yourTableView setDataSource:self];

    [yourTableView sizeToFit];
}

Also, you have to do this after reloadTable, if you do that somewhere.

This will resize your table exactly to height which is a sum of its cells height!

查看更多
闹够了就滚
4楼-- · 2019-02-02 01:45

Add an observer for the contentSize property on the table view, and adjust the frame size accordingly

In viewDidLoad add the following line of code

[your_tableview addObserver:self forKeyPath:@"contentSize" options:0 context:NULL];

then in the callback:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
         CGRect frame = your_tableview.frame;
         frame.size = your_tableview.contentSize;
         your_tableview.frame = frame;
    }

Hope this will help you.

查看更多
混吃等死
5楼-- · 2019-02-02 01:52

if I understand it correctly, you should be able to add up the height for each row and adjust the tableview height based on that:

CGFloat tableHeight = 0.0f;
for (int i = 0; i < [_stepsArray count]; i ++) {
    tableHeight += [self tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
}
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, tableHeight);

you can do this immediately after [tableView reloadData]

查看更多
\"骚年 ilove
6楼-- · 2019-02-02 01:52

Set a height constraint on the table, connect to an outlet, and add the following to your view controller:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    tableViewHeightContraint.constant = tableView.contentSize.height
}
查看更多
戒情不戒烟
7楼-- · 2019-02-02 01:56

You can try like this,

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
        NSLog(@"%f",your_tableView.contentSize.height);
    }
}

In Swift 3.0

func tableView(_ tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.row == tableView.indexPathsForVisibleRows?.last?.row {
        print("\(tableView.contentSize.height)")
    }
}
查看更多
登录 后发表回答