【问题标题】:UICollectionView cell for item at index path based on variable基于变量的索引路径中项目的 UICollectionView 单元格
【发布时间】:2017-05-15 12:50:11
【问题描述】:

我想根据核心数据调用 hasImage 中的变量注册第二个单元格。当变量存在时,我希望集合视图使用媒体单元格,当它不存在时,我希望它使用常规单元格。

最初我在索引路径的单元格中使用了以下内容:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath :
    IndexPath) -> UICollectionViewCell{

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as!
    ShareCell

    let friend = fetchedResultsController.object(at: indexPath) as! Friend

    cell.cell = friend.lastMessage

    return cell

}

上面的代码运行良好。

以下是我创建新逻辑的尝试。

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath :
    IndexPath) -> UICollectionViewCell{

    var cellprop: Cell? {
        didSet{

        if cellprop?.hasImage == false {

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as!
            ShareCell

            let friend = fetchedResultsController.object(at: indexPath) as! Friend

            cell.cell = friend.lastMessage

            return cell
        }
        }
    }

}

我得到的错误是 void 函数中的意外非 void 返回值。

有什么建议吗?

编辑:

ShareCell 工作得非常好,它被归类为 BaseCell,除了它是一个普通的 collectionViewCell 之外,它还被定义为

class BaseCell: UICollectionViewCell {

override init(frame: CGRect){
    super.init(frame: frame)
    setupViews()
}

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

func setupViews(){
    backgroundColor = UIColor.white
}
}

【问题讨论】:

  • cell.cell 是什么意思?

标签: ios swift xcode uicollectionview


【解决方案1】:

首先,您的问题令人困惑!

我假设您要根据最后一条消息是否有图像来选择媒体单元格或常规单元格。
如果是这种情况,那么这里是解决方案:

 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath :
    IndexPath) -> UICollectionViewCell{

    let friend = fetchedResultsController.object(at: indexPath) as! Friend
    if friend.lastMessage.hasImage {
        let mediaCell = collectionView.dequeueReusableCell(withReuseIdentifier: mediaCellId, for: indexPath) as!
    MediaCell
        mediaCell.message = friend.lastMessage
        return mediaCell
    }
    else {
        let regularCell = collectionView.dequeueReusableCell(withReuseIdentifier: regularCellId, for: indexPath) as!
    RegularCell
        regularCell.message = friend.lastMessage
        return regularCell
    }
}

【讨论】:

  • 此解决方案不会产生任何错误。但是,集合视图中的单元格要么全部作为常规单元格返回,要么作为媒体单元格返回。
【解决方案2】:

您已经为cellprop 变量描述了setter,但它从未在您的代码中被调用。而你的return 语句只在那个死范围内

【讨论】:

    【解决方案3】:

    编译器错误是因为您试图从 didSet 方法中返回单元格。你不能这样做,因为 didSet 是一个 void 函数。

    无论如何,您不应该在这种情况下使用 didSet。

    您应该在情节提要中创建两个具有唯一重用标识符的原型单元。

    在 cellForItemAt 中,您应该使用 dequeueReusableCell 创建这些单元格之一,设置单元格的属性,然后返回它。这是一个基本的 if-else 语句,仅此而已。

    if条件需要检查hasImage,这意味着您需要访问与您正在创建的单元格对应的Core Data实体。

    【讨论】:

    • 我没有使用情节提要,并且在访问核心数据实体时遇到问题。
    【解决方案4】:

    您需要在 cellForItemAt indexPath 中返回单元格。

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath :
        IndexPath) -> UICollectionViewCell{
            var cellprop: Cell? 
            if cellprop.hasImage == false {
    
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as!
                ShareCell
    
                let friend = fetchedResultsController.object(at: indexPath) as! Friend
    
                cell.cell = friend.lastMessage
    
                return cell
            }
           else{
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId2, for: indexPath) as!
                RegulerCell
                return cell
            }
    
    
    }
    

    【讨论】:

    • 我无法访问成员 hasImage 并且它肯定存在。
    • NSManaged 对象称为 Cell,其中一个属性是 .hasImage 但我无法访问它。它说实例成员不能用于 Cell 类型。
    • 这会崩溃,因为您使用相同的重用标识符来返回不同的单元格类型。
    • 我认为这只是基于假设您必须更改标识符的指导,因为您想在一个表格中使用两个单元格
    • 我还是不能返回单元格,错误是现在不能将ShareCell类型的返回表达式转换为返回类型Cell
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多