How do I resize the UITableView's height dynamically?

后端 未结 3 770
有刺的猬
有刺的猬 2020-12-31 22:05

In my app, I would like to resize the tableview\'s height when it\'s in edit mode vs when it\'s not (in order to make room for editing controls below the table view)

相关标签:
3条回答
  • 2020-12-31 22:16

    I found that manipulating the "bounds" property can result in some unexpected behavior when you have a floating table inside another view. Sometimes the table expands upward when increasing the height, even though the origin is still 0,0.

    The "frame" property might be more effective:

    CGRect tvframe = [tableView frame];
    [tableView setFrame:CGRectMake(tvframe.origin.x, 
                                    tvframe.origin.y, 
                                    tvframe.size.width, 
                                    tvframe.size.height + 20)];
    
    0 讨论(0)
  • 2020-12-31 22:18

    You need to set the bounds of the tableview:

    CGRect tvbounds = [tableView bounds];
    [tableView setBounds:CGRectMake(tvbounds.origin.x, 
                                    tvbounds.origin.y, 
                                    tvbounds.size.width, 
                                    tvbounds.size.height + 20)];
    
    0 讨论(0)
  • 2020-12-31 22:22

    You can implement the table view delegate's tableView:willBeginEditingRowAtIndexPath: and tableView:didEndEditingRowAtIndexPath: to figure out when a given row (and thus the table) goes into and out of editing mode, respectively.

    From there, you can just tweak the value of the frame.size.height property of the table view (optionally inside a UIView animations block) as appropriate. It may also be helpful to check the editing property of the table view as a whole inside the delegate methods, in case you receive multiple calls to either delegate method before receiving any calls to their complementary methods.

    More info:

    • UITableView: editing property
    • UITableViewDelegate: tableView:willBeginEditingRowAtIndexPath: method (didEndEditing is also in this document)
    0 讨论(0)
提交回复
热议问题