大家好。我 1 个月前开始学习编程,所以如果我没有很好地解释我的问题,请友善:)
我的项目由主要组成UITableView
。在每个单元格中UITableView
, 我有一个UICollectionView
(水平滚动)。
图 1:主视图 https://i.stack.imgur.com/QKZqp.png
The width每个UICollectionViewCell
与整个相同UITableViewCell
。我的第一个问题是关于调整高度的大小UITableViewCell
(这将取决于UICollectionView
本身及其顶部内容的大小)。
图 2:集合视图 https://i.stack.imgur.com/lF5sn.png
这必须自动完成。事实上,UICollectionViewCell
s will 高度不一样,所以当用户滚动时UICollectionView
水平方向上,一个新的UICollectionViewCell
将出现(具有不同的高度)并且高度UITableViewCell
将不得不适应。
我遇到的第二个问题是关于调整大小UICollectionViewCell
,事实上我不会提前知道它的高度是多少(宽度与UITableView
)。我应该从笔尖导入内容。
现在这是我的 ViewController 文件,
变量:
@IBOutlet weak var tableView: UITableView!
var storedOffsets = [Int: CGFloat]()
the UITableView
扩展:创建单元格UITableView
并设置代表UICollectionView
在里面
extension IndexVC: UITableViewDelegate, UITableViewDataSource {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 6 //Temp
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCellWithIdentifier("CellCollectionView") as? CellPost {
let post = self.posts[indexPath.row]
cell.configureCell(post)
return cell
} else {
return CellPost()
}
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
guard let tableViewCell = cell as? CellPost else { return }
tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
tableViewCell.collectionViewOffset = storedOffsets[indexPath.row] ?? 0
}
func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
guard let tableViewCell = cell as? CellPost else { return }
storedOffsets[indexPath.row] = tableViewCell.collectionViewOffset
}
的一部分UICollectionView
:将 Nib/xib 中的视图添加到单元格中
extension IndexVC: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 9 //Temp
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellInCollectionView", forIndexPath: indexPath)
if let textPostView = NSBundle.mainBundle().loadNibNamed("textPostView", owner: self, options: nil).first as? textPostView {
textPostView.configurePost(post.descriptionLbl)
cell.addSubview(textPostView)
textPostView.translatesAutoresizingMaskIntoConstraints = false
cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":textPostView]))
cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view":textPostView]))
}
return cell
}
使单元格的大小与整个单元格的大小相同UICollectionView
extension IndexVC: UICollectionViewDelegateFlowLayout {
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(collectionView.frame.width, collectionView.frame.height)
}
我在专门的课程中创建了这个扩展UITableViewCell
(对于该问题没有用,但如果你们中的任何人想重新创建它)
extension CellPost {
func setCollectionViewDataSourceDelegate<D: protocol<UICollectionViewDataSource, UICollectionViewDelegate>>(dataSourceDelegate: D, forRow row: Int) {
collectionView.delegate = dataSourceDelegate
collectionView.dataSource = dataSourceDelegate
collectionView.tag = row
collectionView.setContentOffset(collectionView.contentOffset, animated:false) // Stops collection view if it was scrolling.
collectionView.reloadData()
}
var collectionViewOffset: CGFloat {
set {
collectionView.contentOffset.x = newValue
}
get {
return collectionView.contentOffset.x
}
}
如果有人想使用这段代码,它效果很好,但我必须对高度进行硬编码UITableView
with a tableView( ... heightForRowAtIndexPath ...)
我尝试了一切来制作UITableViewCell
适应其中的内容(我尝试计算我放入单元格中的笔尖发送的内容的大小,然后将其发送到tableView( ... heightForRowAtIndexPath ...)
但我无法让它发挥作用。我也尝试过自动布局,但也许我做错了。我还认为问题可能来自于我在手机中导入笔尖的部分。
我也不知道在用户滑动后如何展开单元格UICollectionViewCell
,有什么办法可以在发生这种情况时创建一个事件吗?也许打电话tableView( ... heightForRowAtIndexPath ...)
again?