Eit*_*viv 5 iphone uitableview ios uicollectionview swift
我正在开发一个应用程序,它要求我的 ViewController 中有两种类型的提要 - 水平和垂直。
结构是这样的: ViewController -> UITableView-> 第一部分有水平UICollectionView-> 第二部分有垂直UICollectionView。
我的问题是我无法使UITableView(在 Vertical 内部UICollectionView)的第二部分具有适当的高度。当我向下滚动时,它会在中间中断。
重要的是要说 UICollectionView 单元格高度不是固定的。
我遵循了一些教程和指南,看到了许多用例UITableViewAutomaticDimension,但无法使其工作。
编辑:这是我的故事板看起来怎么样: 我的故事板 这是我的主视图控制器代码:
类 FeedViewController:UIViewController、UITableViewDataSource、UITableViewDelegate {
@IBOutlet 弱变量 tableView:UITableView!
var stationArray = Array()
覆盖 func viewDidLoad() {
super.viewDidLoad()d
}
func numberOfSections(in tableView: UITableView) -> Int {
返回 2
}
func tableView(_ tableView: UITableView, titleForHeaderInSection 部分: Int) -> String? {
退货类别[部分]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection 部分: Int) -> Int {
返回 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
如果 indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FeedHorizontalTableViewCell
*一些代码*
返回单元格
} 别的 {
让 cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2") as! FeedVerticalTableViewCell
*一些代码*
cell2.setNeedsLayout()
返回单元格 2
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection 部分: Int) -> CGFloat {
如果部分 == 1 {
返回 50
}
返回 0
}
func tableView(_ tableView: UITableView,estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
返回 500
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
如果 indexPath.section == 0 {
if let cell = tableView.dequeueReusableCell(withIdentifier: "cell") {
返回 cell.frame.height
}
返回 280
} 别的 {
返回 UITableViewAutomaticDimension
}
}
}
这是我的垂直 UITableViewCell 代码:
类 FeedVerticalTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet 弱变量 collectionView:UICollectionView!
var 站 = Array() {
已设置{
collectionView.reloadData()
}
}
覆盖 funcawakeFromNib() {
super.awakeFromNib()
}
覆盖 func setSelected(_ selected: Bool, 动画: Bool) {
super.setSelected(选中,动画:动画)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection 部分: Int) -> Int {
返回站数
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "feedVerticalCell", for: indexPath) as! FeedVerticalCollectionViewCell
*一些代码*
返回单元格
}
}
在视图控制器中将 UITableView 单元格高度设置为动态后。
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
Run Code Online (Sandbox Code Playgroud)
为您的垂直 collectionView 创建一个子类并覆盖 internalContentSize。
class DynamicCollectionView: UICollectionView {
override func layoutSubviews() {
super.layoutSubviews()
if !__CGSizeEqualToSize(bounds.size, self.intrinsicContentSize) {
self.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
return collectionViewLayout.collectionViewContentSize
}
}
Run Code Online (Sandbox Code Playgroud)在界面生成器中,将 collectionView 的类更改为 DynamicCollectionView(子类 UICollectionView)。
设置 UICollectionViewFlowLayout 的估计单元格大小。
flowLayout.estimatedItemSize = CGSize(width: 1,height: 1)
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
3053 次 |
| 最近记录: |