multiple UICollectionviews inside a UItabelview with sections - swift

后端 未结 1 380
自闭症患者
自闭症患者 2021-01-03 03:44

Basically, I am trying to obtain something similar to the result of this : http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell

how ever, i\'m w

相关标签:
1条回答
  • 2021-01-03 03:49

    You need to know which (tableView) section your collectionView relates to. One way to do this would be to subclass UICollectionView and add a tvSection property to it - in the same way that Ash Furrow uses AFIndexedCollectionView to add an index property.

    But you seem to be using the collectionView tag in place of his index, presumably to avoid subclassing. If so, a similar dodge would be to use the tag of the tableViewCell's contentView to indicate which section the cell is in. Amend the tableView cellForRowAtIndexPath method to do this:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as UITableViewCell
    
        if (indexPath.section == 0){
            var cell2: tableViewCellCollectionView = tableView.dequeueReusableCellWithIdentifier(reuseTableViewCellIdentifier, forIndexPath: indexPath) as tableViewCellCollectionView
            cell2.contentView.tag = indexPath.section
            return cell2
        } else if (indexPath.section == 1){
            let cell3: tableViewCellCollectionView2 = tableView.dequeueReusableCellWithIdentifier(reuseTableViewCellIdentifier2, forIndexPath: indexPath) as tableViewCellCollectionView2
            cell3.contentView.tag = indexPath.section
            return cell3
        }
        cell.contentView.tag = indexPath.section
        return cell
    }
    

    Because the collectionView is added as a subview of the contentView, you can determine the (tableView) section for a given collectionView using superview.tag. So modify your collectionView's cellForItemAtIndexPath to test this:

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let section = collectionView.superview.tag
        if section == 0 {
            let cell: collectionViewCellTableView = collectionView.dequeueReusableCellWithReuseIdentifier(reuseCollectionViewCellIdentifier, forIndexPath: indexPath) as collectionViewCellTableView
            // Now configure from the data...
            var rowData: String = self.genre.objectAtIndex(indexPath.row) as String
            cell.title.text = rowData as String
            return cell
        else {
            let cell2: collectionViewCellTableView2 = collectionView.dequeueReusableCellWithReuseIdentifier(reuseCollectionViewCellIdentifier2, forIndexPath: indexPath) as collectionViewCellTableView2
            // Now configure from the data...
            let userPost: String  = self.radioStat.objectAtIndex(indexPath.row) as String
            cell2.pinImage.image = UIImage(named: userPost)
            return cell2
        }
    }
    

    This will need some polishing; in particular I'm not sure how you wish to map your data (sourceArray, genre and radioStat) to the tableView rows and collectionView items. (I've taken a guess based on your existing code). But this should give you something to work with.

    0 讨论(0)
提交回复
热议问题