【发布时间】:2015-03-22 21:41:46
【问题描述】:
我有一个带有自定义单元格的 UITableView,每个单元格都包含一个水平滚动的 UICollectionView。当表格视图单元被回收时,集合视图的水平滚动位置也随之回收。创建新单元格时是否应该手动重置集合视图滚动位置?如果是这样,有没有办法在每个单元格的基础上保留滚动位置?
自定义 UITableViewCell
class CustomCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
var collectionView:UICollectionView!
var layout = UICollectionViewFlowLayout()
var collectionData = [UIImage]()
let kCellIdentifier = "Cell"
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
layout.minimumLineSpacing = 10.0
layout.minimumInteritemSpacing = 1.0
layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
collectionView.registerClass(ItemCell.self, forCellWithReuseIdentifier: kCellIdentifier)
collectionView.delegate = self
collectionView.dataSource = self
addSubview(collectionView)
}
override func layoutSubviews() {
super.layoutSubviews()
layout.itemSize = CGSize(width: 800, height: bounds.height)
collectionView.frame = bounds
}
}
extension ProjectCell: UICollectionViewDataSource {
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(kCellIdentifier, forIndexPath: indexPath) as! ItemCell
//Reset collectionView scroll position?
return cell
}
}
【问题讨论】: