我希望将标签隐藏在被点击的单元格中,而不是显示图像。但我只想在具有特定索引的单元格已经设置为imageView的情况下执行此操作。如果单元格设置为imageView或未设置为imageView,那么寻址和存储单元格的最佳方式是什么?如何使用prepareForReuse方法?
到目前为止,这就是我的方法,但是当单元被重用时。图像在滚动时显示在其他单元格中。
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
println("user tapped on door number \(indexPath.row)")
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! MyCollectionViewCell
if (cell.myLabel.text == "1") {
one = true
if(seven = true) {
if (cell.myLabel.hidden) {
cell.myLabel.hidden = false
cell.MyImageView.image = nil
}
else {
cell.myLabel.hidden = true
cell.MyImageView.image = UIImage(named:"1")!
}
}
}发布于 2015-11-28 22:38:10
您没有说您的集合视图是否正好有7个单元格,或者集合中是否可以有"N“(例如100)个单元格,所以如果这是我的问题并且我必须解决它,我会将"seven”单元格的状态设置为类的属性(例如"var sevenState : Bool"),然后我可以根据sevenState是什么来显示其他单元格的按钮或图像。
发布于 2015-11-29 00:19:22
在我的应用程序中,我必须根据索引路径配置一个UICollectionReusableView,如果indexPath有一个特定值,那么我会发送一个用于设置标签和图像的数组。
我在自定义的UICollectionReusableView中使用了一个函数,如果我用一个数组调用它,它会填充标签和图像,如果我用nil调用它,它会重置这些。
func collectionView(collectionView: UICollectionView!, viewForSupplementaryElementOfKind kind: String!, atIndexPath indexPath: NSIndexPath!) -> UICollectionReusableView! {
.... [logic around selecting index path based on data returned]
....
if filteredEvents != nil{
reusableView.populateCalendarDayDates(sortedEvents)
}else{
reusableView.populateCalendarDayDates(nil)
}在自定义UICollectionReusableView的函数中,在可能更新标签之前,我将标签重置回默认值:
func populateCalendarDayDates(arrayEvents: NSArray?){
let firstDayTag = tagStartDay()
var dayDate = 1
for var y = 1; y < 43; y++ {
let label = self.viewWithTag(y) as! BGSCalendarMonthLabel
label.delegate = callingCVC
label.backgroundColor = UIColor.clearColor()
label.textColor = UIColor.whiteColor()
label.text = ""通过将以下代码移动到自定义UICollectionReusableView中的prepareForReuse,您可以获得相同的效果,而且可读性可能会更好一些:
override func prepareForReuse() {
super.prepareForReuse()
for var y = 1; y < 43; y++ {
let label = self.viewWithTag(y) as! BGSCalendarMonthLabel
label.backgroundColor = UIColor.clearColor()
label.textColor = UIColor.whiteColor()
label.text = ""
}
}希望这能有所帮助。
https://stackoverflow.com/questions/33972416
复制相似问题