簡體   English   中英

在UICollectionViewCell內設置UILabel

[英]Setting a UILabel inside a UICollectionViewCell

所以事情是我在TableView的CollectionView內有4個UICollectionViewCells。 (我將TableViewController設置為CollectionView的DataSource和Delegate)。

現在,我在CKRecord中存儲了一個由4個元素組成的字符串數組。 如何在4個單元格內設置標簽,以便它們顯示數組的每個字符串?

看起來是這樣的:

func collectionView(collectionView: UICollectionView,
                    numberOfItemsInSection section: Int) -> Int {

    return 4
}


func collectionView(collectionView: UICollectionView,
                        cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CvCell

        let poll = polls[indexPath.row] // polls is a [CKRecord]()
        let labelContent = poll["4strings"] as? [String]


        cell.cellLabel.titleLabel?.text = labelContent

        return cell

    }

現在顯然,這將行不通,因為我基本上將每個單元格的標簽設置為數組本身。 如何編寫遍歷每個CvCell標簽的for循環,或者如何指定第三個單元格的標簽以將其設置為labelContent [2]的值?

更新:

完全忘記提了,雲中的數據結構基本上是這樣的:

array1 = [1a, 1b, 1c, 1d]

array2 = [2a, 3b, 2c, 2d]

array3 = [3a, 2b, 3c, 3d]

array4 = [4a, 4b, 4c, 4d]

而且,如果我嘗試執行@ user3353890提出的建議,則會為我的表視圖提供以下結果:

tableview1cell - collectionview1 : [1a, 2b, 3c, 4d] -> these are the collectionviewcell labels

tableview2cell - collectionview2 : [1a, 2b, 3c, 4d]

tableview3cell - collectionview3 : [1a, 2b, 3c, 4d]

tableview4cell - collectionview4 : [1a, 2b, 3c, 4d]

但是我想要的是:

tableview1cell - collectionview1 : [1a, 1b, 1c, 1d]

tableview2cell - collectionview2 : [2a, 2b, 2c, 2d]

tableview3cell - collectionview3 : [3a, 3b, 3c, 3d]

tableview4cell - collectionview4 : [4a, 4b, 4c, 4d]

對不起,我很難解釋這一點,但是我希望有人得到我想要做的事情?

首先,您不會返回一個單元格。 您最終將返回4個單元格,因為您規定在numberOfItemsInSection方法的每個節中都需要4個項目。

func collectionView(collectionView: UICollectionView,
                numberOfItemsInSection section: Int) -> Int {
    return 4
}

cellForItemAtIndexPath協調您要如何顯示每個單元格。 因為您聲明一個節中有4個項目,所以此方法將被調用4次,每次在該節中被調用為項目時將返回1個單元格。

func collectionView(collectionView: UICollectionView,
                    cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CvCell

    let poll = polls[collectionView.tag] // polls is a [CKRecord]()
    let labelContent = poll["4strings"] as? [String]

    // This should give you the string that you want.
    let myString = labelContent[indexPath.row]

    // Display the string in the label.
    cell.cellLabel.titleLabel?.text = myString

    return cell

}

設置labelContent數組后,通過將indexPath.row傳遞到數組中,在每個索引處獲取myString。 因此,對於第一個單元格(0索引),它為您提供了數組中的第一個字符串(0索引)。

編輯

創建輪詢時,請使用indexPath.section以便在顯示數據時保持所有數組的正確順序。

let poll = polls[collectionView.tag]

編輯2

在tableView cellForRowAtIndexPath方法中,創建單元格時,將單元格的collectionView.tag設置為indexPath.section。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomCell
    cell.collectionView.delegate = self
    cell.collectionView.dataSource = self
    cell.collectionView.tag = indexPath.section
    return cell
}

然后,當您使collectionView出隊時,可以通過調用collectionView.tag(如上所示)來訪問適當的數組,以獲取tableViewCell的indexPath。

更好的解決方案,因此您不必將collectionView嵌套在tableView中:

這樣可以為您提供所需的部分數量。 “輪詢”數組中每個數組的一部分:

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return polls.count
}

這將為每個相應部分獲取子數組,並允許該collectionView部分中的項目數與該數組中的項目數相對應:

func collectionView(collectionView: UICollectionView,
                numberOfItemsInSection section: Int) -> Int {
    let poll = polls[section] as? [String]
    return poll.count
}

在此處顯示您的collectionView單元格:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CvCell

    let poll = polls[indexPath.section] // polls is a [CKRecord]()
    let labelContent = poll["4strings"] as? [String]

    // This should give you the string that you want.
    let myString = labelContent[indexPath.row]

    cell.cellLabel.titleLabel?.text = myString

    return cell

}

暫無
暫無

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

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