Swift dynamic tableview height based on content

寵の児 提交于 2019-12-25 14:25:07

问题


Did a search and the only thing I can find on here are for dynamic cell heights. I'm new to swift, and I'd like to have a tableview where the height of the tableview adjusts according to the number of rows. Thanks in advance for the help.


回答1:


I can't think of a good reason to do this but...

You must know how many items are in the tableview since it's your datasource (presumably whatever number you're returning in collectionView:numberOfItemsInSection.

So you could adjust the size like so:

let totalSizeOfCells = (tableView.rowHeight * CGFloat(numOfItems.count))
tableView.frame = CGRectMake(0, 0, self.view.frame.width, totalSizeOfCells)

Where numOfItems.count is the number of cells you have.



来源:https://stackoverflow.com/questions/35189482/swift-dynamic-tableview-height-based-on-content

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