【发布时间】:2015-02-21 23:14:48
【问题描述】:
每个 UICollectionViewCell 都有自己的按钮连接到以下操作:
@IBAction func dropDown(sender:UIButton){
var pt = sender.bounds.origin
var ptCoords : CGPoint = sender.convertPoint(pt, toView:sender.superview);
var ptCoords2 : CGPoint = sender.convertPoint( ptCoords, toView: collectionView!);
var cellIndex: NSIndexPath = self.collectionView!.indexPathForItemAtPoint(ptCoords2)!
//var i : NSInteger = cellIndex.row;
//var i2 : NSInteger = cellIndex.section;
var selectedCell = collectionView?.cellForItemAtIndexPath(cellIndex) as CollectionViewCell!
selectedCell.button.backgroundColor = UIColor.blackColor()
for (var i = 0; i < 3; i++){
var textView : UITextView! = UITextView(frame: CGRectMake(self.view.frame.size.width - self.view.frame.size.width/1.3, CGFloat(50 + (30*(i+1))), CGRectGetWidth(self.view.frame), CGFloat(25)))
textView.backgroundColor = UIColor.whiteColor()
selectedCell.contentView.addSubview(textView)
}
}
我想要做的是仅向被点击的单元格添加 3 个子视图。子视图已成功添加,但一旦我滚动,进入视图并对应于先前设置的 indexPath 的单元格将加载 3 个子视图。我认为这是由于 dequeueReusableCellWithReuseIdentifier 方法造成的,但我无法找到解决方法。我考虑删除scrollViewDidScroll 上的子视图,但理想情况下,我希望将视图保留在其父单元格上,直到再次点击按钮。
编辑:
好的,我放弃了整个 convertPoint 方法,现在根据按钮标签获取单元格索引:
var selectedCellIndex : NSIndexPath = NSIndexPath(forRow: cell.button.tag, inSection: 0)
var selectedCell = collectionView?.cellForItemAtIndexPath(selectedCellIndex) as CollectionViewCell!
无论如何,当我尝试仅将子视图添加到所选索引处的单元格时,子视图会重复。
编辑: 我创建了一个包含键值的字典来跟踪每个单元格的状态,如下所示:
var cellStates = [NSIndexPath: Bool]()
for(var i = 0; i < cellImages.count; i++){
cellStates[NSIndexPath(forRow: i, inSection: 0)] = false
}
由cellStates[selectedCellIndex] = true 在dropDown 函数中设置。然后在cellForItemAtIndexPath 函数中,我做了以下检查:
if(selectedIndex == indexPath && cellStates[indexPath] == true){
for (var i = 0; i < 3; i++){
var textView : UITextView! = UITextView(frame: CGRectMake(cell.frame.size.width - cell.frame.size.width/1.3, CGFloat(50 + (30 * (i+1))), CGRectGetWidth(cell.frame), CGFloat(25)))
textView.backgroundColor = UIColor.whiteColor()
cell.contentView.addSubview(textView)
println("display subviews")
println(indexPath)
}
} else {
println("do not display subviews")
println(indexPath)
}
return cell
其中selectedIndex,通过dropDown 函数设置的活动单元格的NSIndexPath 与正在创建的单元格的indexPath 进行比较,并检查cellState 中的true。
仍然没有运气 - 子视图仍然显示在回收的单元格上。我应该提到“显示子视图”和“不显示子视图”在滚动时被正确记录,因此条件语句被成功评估。
我的(...hack of a...)解决方案!
可能破坏了一堆最佳编码实践,但我为所有创建的子视图分配了标签,在 cellForItemAtIndexPath 方法的开头删除它们,如果cellState 条件返回true,则重新创建它们。
【问题讨论】:
标签: ios swift uicollectionview uicollectionviewcell subview