【发布时间】:2017-09-15 02:49:02
【问题描述】:
我正在使用UITableViewController 并将UICollectionView 放在表格视图单元格的第二行。问题是我如何动态获得 heightForRowAt 。
我尝试过使用return UITableViewAutomaticDimension。但不工作。
非常感谢任何帮助。
viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = self.tableView.rowHeight
self.tableView.rowHeight = UITableViewAutomaticDimension
}
UITableViewController
选项 1
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return self.tableView.bounds.width + 15
}
选项 2
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
目前,我对退货数量进行了硬编码。
UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 15
}
UICollectionViewDelegate、UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let layout = collectionViewLayout as! UICollectionViewFlowLayout
layout.minimumLineSpacing = 2.5
layout.minimumInteritemSpacing = 2.5
itemWidth = (collectionView.bounds.width - 5.0) / 3.0
return CGSize(width: itemWidth, height: itemWidth)
}
这是使用 return self.tableView.bounds.width + 15 的 UI 图像:
这是使用 return UITableViewAutomaticDimension 的 UI 图像:
这是约束的图像:
【问题讨论】:
-
你对 UICollectionView 的限制是什么?
-
@TusharSharma 查看更新的问题。全部设置为 0。
-
您能展示一下您是如何使用自动尺寸的吗?我的意思是您执行了哪些步骤?
-
我已经更新了问题。目前我正在做的是设置
return self.tableView.bounds.width + 15。获得长高。如果我使用UITableViewAutomaticDimension,它将是普通表。 -
@TusharSharma,我已经更新了问题。
标签: ios swift uitableview uicollectionview