基于变量的索引路径项的UICollectionView单元格

时间:2017-05-15 12:50:12

标签: ios swift xcode uicollectionview

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

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

In [119]: def test_loop():
    sorted_array = []
    for vals in sortby:
        sorted_array.append(mydict[vals])
    return np.array(sorted_array)
   .....: 

In [120]: def test_list_compres():
    return np.array([mydict[i] for i in sortby])
   .....: 

In [121]: %timeit test_list_compres
10000000 loops, best of 3: 20 ns per loop

In [122]: %timeit test_loop
10000000 loops, best of 3: 21.3 ns per loop

In [123]: %timeit test_list_compres
10000000 loops, best of 3: 20.1 ns per loop

In [124]: %timeit test_loop
10000000 loops, best of 3: 21.9 ns per loop

上面的代码非常合适。

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

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

}

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

有什么建议吗?

编辑:

ShareCell工作得很好,并且它被子类化为BaseCell,后者定义为它是正常的collectionViewCell

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
        }
        }
    }

}

4 个答案:

答案 0 :(得分:0)

您已经为cellprop变量描述了setter,但它从未在您的代码中调用过。并且您的return语句仅在该死区范围内

答案 1 :(得分:0)

您需要在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
        }


}

答案 2 :(得分:0)

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

无论如何,你不应该在这个上下文中真正使用didSet。

您应该在故事板中创建两个具有唯一重用标识符的原型单元格。

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

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

答案 3 :(得分:0)

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

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

 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
    }
}