簡體   English   中英

UICollectionView中的多個單元格

[英]multiple cells in UICollectionView

我有一個UICollectionView並有兩種單元格類型。 ShareCell和ShareCellMedia。 我有它,以便僅當屬性.hasImage為true時才返回ShareCellMedia。 但是,在索引路徑的cellforitem中實現邏輯時,在這種情況下,它會以ShareCell或ShareCellMedia的形式返回所有單元格,因為我使用的一個單元格的數據應該是媒體單元格,而其他單元格是常規的。

下面是單元格中索引路徑處的代碼

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

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

return UICollectionViewCell()
}

有什么建議么?

您是否在獲取正確的朋友對象? 當您使用indexPath ,而不是indexPath.row

我在索引路徑處的cellforitem並不是問題的原因。

感謝@DonMags的部分評論,我能夠推斷出我用於消息的函數沒有正確實例化變量.hasImage。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM