Adjust UITableView height dynamically

邮差的信 提交于 2020-01-14 04:43:08

问题


How can I adjust the UITableView height dynamically? Let's say I have 3 rows and each row has a height of 65. When a new row is added to the UITableView i want it to adjust it's height dynamically to fit the new row. And vice versa when a row is deleted to shrink.


回答1:


Edit : Misread

The thing depends on the (height of table cell row * number of rows in table) as the tableview.frame.size.height.

Well I would suggest something like this..

-(void)correctHeight
{

    //Note : if all cells are having common value
    CGFloat heightOfCell=[table rowHeight];

    CGFloat expectedHeightOfTable=0;
    for (int i=0; i< [table numberOfSections]; i++) {
        //header section
        CGFloat heightOfHeaderView=[table headerViewForSection:i].frame.size.height;
        expectedHeightOfTable = expectedHeightOfTable +heightOfHeaderView;

        //content section
         expectedHeightOfTable = expectedHeightOfTable + heightOfCell*[table numberOfRowsInSection:i];
    }
    [table setFrame:CGRectMake(table.frame.origin.x, table.frame.origin.y, table.frame.size.width,expectedHeightOfTable)];


}

This will consider header and each row content and all value populated prormatically..So dont need to add some hardcoded values




回答2:


The tableview height can be set by,

[tableView setFrame:CGRectMake(0,0,width,height)];

But for example if u set the height as 150 and you 5 rows of height 60px using tableView:heightForRowAtIndexPath: then the tableView will become scrollable as UITableView is a sub class of UIScrollView.




回答3:


As said in comment, Weird structure, but if you insist, that's okay. I suggest to use a simple UITableView only.

Assume you have IBOutlet to UITableView and UIScrollView...

Before adding codes in viewWillAppear, add:

float cellHeight = 65.0f;
int numberOfCells = 3;

In - (void)viewWillAppear:animated, add the following after [super viewWillAppear:animated]:

// remember fill the table with data using UITableViewDataSource's functions first
// refresh UITableView data
[tableView reloadData];
// resize UITableView
[tableView setFrame:CGRectMake(0, 0, tableView.frame.size.width, cellHeight * numberOfCells)];
// make UIScrollView the same size as UITableView
[scrollView setContentSize:tableView.frame.size];
[scrollView setNeedsDisplay];
// remember to lock tableView's scrolling, either in code / Interface Builder
[tableView setScrollEnabled:NO];

And in UITableViewDelegate's delegate methods:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return numberOfCells;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   return cellHeight;
}

Last, if you wish to double verify if any of the table cell is out of sight, you can use:

- (NSArray *)visibleCells


来源:https://stackoverflow.com/questions/23240761/adjust-uitableview-height-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!