繁体   English   中英

麻烦使uicollectionview单元出队(swift 3)

[英]trouble dequeuing uicollectionview cells (swift 3)

我在uicollectionview中使多个单元出队时遇到麻烦。 有人可以逐步解释从注册到出队的过程。 我找不到任何有关在一个集合视图中加载两个或更多uicollectionview单元的在线信息。 同样,这是基于coreadata中的变量在单个collectionview中加载不止一种类型的单元格。

这是我到目前为止所拥有的。

这是我注册细胞的地方

    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
    }

}

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

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

并检查或打印您的标识符

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

如果是这样,那么您必须像这样在viewdidload中注册笔尖单元

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

另外,加载单元时,必须确保使用相应的ReuseIdentifier

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM