【发布时间】:2017-01-03 14:30:30
【问题描述】:
我有一个 UITableView,里面有两个UICollectionViews。
我的要求是我必须先水平滚动UICollectionView。
我必须垂直滚动UITableView,因为我不想滚动第二个UICollectionView。
在图像中你可以看到第一个(标题'文件夹')UICollectionView我必须水平滚动,第二个(标题'产品')UICollectionView我不想垂直滚动,因为我必须滚动完成@ 987654329@.
collectionView 都在 TableViewCell 中,我已禁用第二个 UICollectionView 的滚动。
我必须添加和删除一个产品,然后我该如何设置内容应该出现的TableViewSecond 单元格高度。
这是我的代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (0 == indexPath.section) {
var addFolderCell = tableView.dequeueReusableCell(withIdentifier: "AddFolderCell") as? AddFolderCell
if(nil == addFolderCell) {
let nib:Array = Bundle.main.loadNibNamed("AddFolderCell", owner: self, options: nil)!
addFolderCell = nib[0] as? AddFolderCell
}
addFolderCell?.collectionView.delegate = self
addFolderCell?.collectionView.dataSource = self
return addFolderCell!
} else {
let identifier = "CreateFolderCell"
var createFolderCell = tableView.dequeueReusableCell(withIdentifier: identifier) as? CreateFolderCell
if(createFolderCell == nil) {
let nib:Array = Bundle.main.loadNibNamed("CreateFolderCell", owner: self, options: nil)!
createFolderCell = nib[0] as? CreateFolderCell
}
return createFolderCell!
}
}
//Height for row.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//let index : NSIndexPath = NSIndexPath(row: 0, section: 1)
if 0 == indexPath.section {
return 135
}
return 300
}
//页眉高度。
func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
return 20
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionsArray[section] as? String
}
}
extension GroupDataView : UICollectionViewDelegate,UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AddFolderCollectionCell", for: indexPath) as! AddFolderCollectionCell
//cell.backgroundColor = UIColor.white
cell.layer.borderWidth = 0.4
cell.layer.borderColor = UIColor.lightGray.cgColor
cell.layer.cornerRadius = 2
//CreateCardView(viewCorner: cell.cardView) //cell.cardView
return cell
}
}
//MARK:集合视图布局
extension GroupDataView : UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 90, height: 130)
}
}
我能做什么?如何设置 tableView 的高度?
【问题讨论】:
-
tableView.rowHeight ?如故事板中定义的那样?
-
是的,但是我该如何设置,因为第二个单元格高度根据第二个 collectionView 内容大小。谢谢
-
看起来你的方向是正确的,因为重写 ViewLayout 方法应该可以完成工作。问题是将集合视图单元格的高度传递给表格视图单元格高度?
-
是的。我能怎么做?谢谢
-
一个 UITableViewCell 持有一个 UICollectionView,现在你的用例是当你删除一个 UITableViewCell(持有 UICollectionView)你不知道 UITableViewCell 的高度取决于 UICollectionViewCell 高度?
标签: ios swift uitableview uicollectionview contentsize