基于内容的 Swift 动态 tableview 高度 [英] Swift dynamic tableview height based on content

查看:60
本文介绍了基于内容的 Swift 动态 tableview 高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进行了搜索,我在这里唯一能找到的是动态单元格高度.我是 swift 的新手,我想要一个 tableview,tableview 的高度根据行数进行调整.预先感谢您的帮助.

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.

推荐答案

我想不出这样做的充分理由,但是...

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

您必须知道 tableview 中有多少项,因为它是您的数据源(大概是您在 collectionView:numberOfItemsInSection 中返回的任何数字.

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)

其中 numOfItems.count 是您拥有的单元格数量.

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

这篇关于基于内容的 Swift 动态 tableview 高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆