【问题标题】:trouble dequeuing uicollectionview cells (swift 3)麻烦出队 uicollectionview 单元格(swift 3)
【发布时间】:2017-05-17 07:25:17
【问题描述】:

我在 uicollectionview 中将多个单元格出列时遇到问题。 有人可以逐步解释从注册到出队的过程。我在网上找不到与在一个集合视图中加载两个或多个 uicollectionview 单元格有关的任何内容。同样,这是为了基于 coreadata 中的变量在单个集合视图中加载多种类型的单元格。

这是我目前所拥有的。

这里是我注册单元格的地方

    collectionView?.register(ShareCell.self, forCellWithReuseIdentifier: cellId)
    collectionView?.register(ShareCellMedia.self, forCellWithReuseIdentifier: mediaCellId)

这是我在索引路径中的项目单元格

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

    let friend = fetchedResultsController.object(at: indexPath) as! Friend
    if (friend.lastMessage?.hasImage)! == true {
        let mediaCell = collectionView.dequeueReusableCell(withReuseIdentifier: mediaCellId, for: indexPath) as!
        ShareCellMedia
        mediaCell.cell = friend.lastMessage
        return mediaCell

    }else{


        let regularCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as!
        ShareCell
        regularCell.cell = friend.lastMessage
        return regularCell
    }

}

【问题讨论】:

  • 您发布的代码看起来不错。你没想到会发生什么?您确定在使用之前设置了 cellId 和 mediaCellId 吗?何时注册单元格,您确定此时 collectionView 不为零吗?

标签: ios swift xcode uicollectionview


【解决方案1】:

如果您使用的是原型静态单元格,则无需注册该单元格,或者如果您使用的是带有 xib 的单元格,您可以像这样注册

     collectionView.register(UINib(nibName: "name here",bundle:nil), forCellWithReuseIdentifier: "identifier")

并检查或打印您的标识符

【讨论】:

  • 对不起我的英语不好
【解决方案2】:

您是否正在使用 xibs 创建您的单元格?

如果是这样 那么你必须像这样在 viewdidload 中注册 nib 单元格

 UINib *cellNib = [UINib nibWithNibName:@"yourCellNib" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"TheNibCellIdentifier"];

您还必须确保在加载单元格时使用相应的ReuseIdentifier

yourCellNib*cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TheNibCellIdentifier"  forIndexPath:indexPath];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 2015-02-15
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多