UITableView: changing footer view's size programmatically doesn't work

时间秒杀一切 提交于 2019-12-03 11:11:12

问题


My table view's footer is a view that shows some tweets from a user, so I do not know its height until I got the tweets, I created a FooterViewController that has a method refreshTweets, I add it in viewDidLoad:

FooterViewController *controller = [[FooterViewController alloc] initWithNibName...];
[[self tableView] setFooterView:[controller view]];
[controller refreshTweets];

in refreshTweets method, I read tweets and calculate the total height, and reset view(the footer)'s height:

self.view.frame = newFrame;

but it does not make sense, the footer's height is still the height I set in Interface Builder, is there anything missing or any alternative way of doing this?

Thanks!

edit: I can change the view's size to be small enough in interface builder, and it will be enlarged after calculating tweets' height and setting view.frame, but the table view still thinks its footer has previous height, that is, the extra space is outside of the tableview and I can only see it if I drag the table up.


回答1:


myTable.tableFooterView = myFooterView;

Re-assign your footer view to the table. The table will recognize it has a new footer and re-do whatever layout needs to occur for the proper size of the footer.




回答2:


In order to change the frame of footer view here is the simple tweak:-

In Swift:-

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 0.5
}


func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView()
    let footerChildView = UIView(frame: CGRect(x: 60, y: 0, width: tableView.frame.width - 60, height: 0.5))
    footerChildView.backgroundColor = UIColor.darkGray
    footerView.addSubview(footerChildView)
    return footerView
}


来源:https://stackoverflow.com/questions/6672528/uitableview-changing-footer-views-size-programmatically-doesnt-work

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