我想在标题单元格中隐藏标签,而是显示图像。此外,我想存储此状态,并仅在某个其他单元格已显示图像时显示图像。我有一定数量的细胞。 我使用indexPath完成了它,但是在滚动时重用了单元格。 如何处理单元格并存储数据?
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
println("user tapped on cell number \(indexPath.row)")
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! MyCollectionViewCell
if (cell.myLabel.text == "1") {
one = true
if (cell.myLabel.hidden) {
cell.myLabel.hidden = false
cell.MyImageView.image = nil
}
else {
cell.myLabel.hidden = true
cell.MyImageView.image = UIImage(named:"1")!
}
}
答案 0 :(得分:0)
您应该将其保存在某些dataSource中,而不是在单元格中保存状态。
答案 1 :(得分:0)
如果没有在索引路径方法中看到行的单元格,我将假设您没有可以变异的对象数组...否则您可以将变量添加到这些对象而不是使用可变字典并将其设置为true
你需要一个可变字典
var selectedIndexes:NSMutableDictionary = NSMutableDictionary()
然后选择您的单元格
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
println("user tapped on cell number \(indexPath.row)")
let keystring = String(format:"%i", indexPath.row)
if self.selectedIndexes[keystring] {
println("cell has been selected before")
// maybe you want to revert the behaviour?
}
else {
self.selectedIndexes.setObject(keystring, forKey: keystring)
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! MyCollectionViewCell
// The criteria wasn't clear when to do you hide code... so whatever you'd like the behaviour to be, check / do it here.
}
}
然后在
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let keystring = String(format:"%i", indexPath.row)
if self.selectedIndexes[keystring] {
// layout your cell in it's selected state
}
else {
// your normal unselected cell
}
}
正如我在评论中所说,目前尚不清楚你何时想要某些行为......但一个简单的原则是根据与索引路径行相关的键将项添加到字典中。
希望有所帮助