首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UiCollectionView单细胞设计

UiCollectionView单细胞设计
EN

Stack Overflow用户
提问于 2015-12-02 14:02:39
回答 3查看 288关注 0票数 0

继续我的UICollectionView学习经验,我被困在最后一个问题上。

我的UIcollection有5个切片,每个有8个细胞。我使用selectCellForIndexPath来加载单元格,在一些社区成员的帮助下,我已经解决了其中的一个问题。

我现在遇到的问题是,某些单元应该被锁定,直到您解锁上一个级别为止。我使用bool值来跟踪未锁定的级别。

所以,我现在要做的是显示第二个图像,并锁定所有尚未解锁的单元格/级别。我使用的是一个自定义单元格,它有一个标签和2个图像视图,其中一个是锁。在方法cellForItemInIndexPath中,我使用这些bool值来检查单元格是否被解锁,从而隐藏锁映像。代码看起来像这样

代码语言:javascript
运行
复制
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CellReuseIdentifier.cell, forIndexPath: indexPath) as! CustomCollectionViewCel

// level label    
cell.levelLabel.text = self.cells[indexPath.row]

// main cell image
cell.imageView.image = UIImage(named: self.cellImages[indexPath.section])

// check unlock image 
if indexPath.section == 0 {
        switch indexPath.row {
           case 0:
           if level1Unlocked {
              cell.lockImage.hidden = true
           }
           case 1:
           if level2Unlocked {
              cell.lockImage.hidden = true
           }
           ...
    }
}
if indexPath.section == 1 {
      switch....
     ....
}

我的自定义手机看起来像这样

代码语言:javascript
运行
复制
import SpriteKit

class CustomCollectionViewCell: UICollectionViewCell {

// MARK: - Properties

/// Background
var imageView: UIImageView!
var lockImageView: UIImageView!

/// Header
var levelLabel: UILabel!

// MARK: - Init
override init(frame: CGRect) {
    super.init(frame: frame)

    /// Image
    imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
    imageView.contentMode = UIViewContentMode.ScaleAspectFit
    imageView.userInteractionEnabled = true
    #if os(tvOS)
    imageView.adjustsImageWhenAncestorFocused = true
    #endif
    contentView.addSubview(imageView)

    /// Lock Image View
    lockImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
    lockImageView.image = UIImage(named: "Lock")
    lockImageView.contentMode = UIViewContentMode.ScaleAspectFit
    lockImageView.userInteractionEnabled = true
    #if os(tvOS)
        lockImageView.adjustsImageWhenAncestorFocused = true
    #endif
    contentView.addSubview(lockImageView)

    /// Level label
    levelLabel = UILabel(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
    levelLabel.font = UIFont(name: Label.Font.noteworthy, size: 22)
    levelLabel.textColor = SKColor.whiteColor()
    //textLabel.backgroundColor = UIColor.whiteColor()
    levelLabel.textAlignment = .Center
    levelLabel.center.y = 0 + 30
    contentView.addSubview(levelLabel)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
   }
}

主要问题如下

1)有些单元格没有显示锁(我检查了上面的代码几次以确保其正确)

2)当我开始滚动,回到一个单元格时,它会显示/不会显示锁图像,尽管它应该显示。

3)我尝试删除/添加锁图像,而不是隐藏,但结果是一样的。

我认为这与可重复使用的细胞有关。是否有人能帮助我,并使我在正确的方向上,我如何可以管理个别的单元格外观,坚持到您重新加载收集视图数据。我是这样做的,因为我是在一个SKScene上的集合视图,所以请不要故事板提示。非常感谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-12-02 18:35:41

在进行了更多的操作之后,似乎唯一可靠的解决方案是使用2个自定义单元格,而不是1个单元格,并试图隐藏某些标签。在我的selectCellForItemAtIndexPath方法中,我现在这样做。

代码语言:javascript
运行
复制
 let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CellReuseIdentifier.cell, forIndexPath: indexPath) as! CustomCollectionViewCell

 let cellLocked = collectionView.dequeueReusableCellWithReuseIdentifier(CellReuseIdentifier.cellLocked, forIndexPath: indexPath) as! CustomCollectionViewCellLocked

而不是根据级别解锁状态返回正确的单元格。

票数 0
EN

Stack Overflow用户

发布于 2015-12-02 14:06:58

你正在经历的是典型的回收电池。这些单元是“去排队的”,所以除非您重新配置它们,否则它们可以显示旧的数据或状态。

解决方案:确保完全配置单元格( cellForItemAtIndexPath中的所有单元格),包括根据需要显式地将lockImage.hidden设置为truefalse

例如,不只是使用if语句将其设置为true

代码语言:javascript
运行
复制
cell.lockImage.hidden = level1Unlocked

如果level1Unocked为真,这将隐藏它,如果不是,则显示它。

顺便说一下,您为每个级别设置的专用变量似乎也是次优设计,但这可能是另一个问题。

票数 0
EN

Stack Overflow用户

发布于 2015-12-02 16:37:38

尝试这样做:

代码语言:javascript
运行
复制
// Lazy load your views in your CustomCell.m
lazy var imageView: UIImageView  = {
    var temporaryImage: UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
    contentView.addSubview(imageView)
    temporaryImage.contentMode = UIViewContentMode.ScaleAspectFit
    temporaryImage.userInteractionEnabled = true
    #if os(tvOS)
    temporaryImage.adjustsImageWhenAncestorFocused = true
    #endif
    return temporaryImage
}()


// Remove all the custom stuff that we added to our subclassed cell
func prepareForReuse() {
    super.prepareForReuse()
    imageView.removeFromSuperview()
    imageView = nil
}

然后从init中删除imageView初始化。

对您拥有的其他两个组件重复使用。我很久以前就知道了,这是在没有故事板帮助的情况下创建customCells的最好方法。

注:我不习惯斯威夫特,所以如果我犯了错,请原谅我

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34044688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档