最新消息:20210917 已从crifan.com换到crifan.org

[已解决]tableview中内嵌的UICollectionView中的UICollectionViewCell不显示

iOS crifan 2849浏览 0评论

已经实现了各个函数,包括:

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

        gLog.verbose("collectionView=\(collectionView), indexPath=\(indexPath)")

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(OptionCollectionViewCellId, forIndexPath: indexPath)

        cell.backgroundColor = OptionCellBackgroundColorNotSelected

        cell.layer.cornerRadius = 10

        cell.layer.borderWidth = 2

        cell.layer.borderColor = UIColor.blueColor().CGColor

        if indexPath.row < self.optionList.count {

            let curOption = self.optionList[indexPath.row]

            let curOptionLabel = UILabel()

            curOptionLabel.text = curOption

            curOptionLabel.font = UIFont.systemFontOfSize(14)

            cell.contentView.addSubview(curOptionLabel)

            constrain(curOptionLabel) {curOptionLabel in

                curOptionLabel.top == curOptionLabel.superview!.top + 5

                curOptionLabel.left == curOptionLabel.superview!.left + 5

                curOptionLabel.center == curOptionLabel.superview!.center

                curOptionLabel.right == curOptionLabel.superview!.right – 5

                curOptionLabel.bottom == curOptionLabel.superview!.bottom – 5

            }

        }

        return cell

    }

效果:

UICollectionView item not show

ios – UICollectionViewCells not appearing in a UICollectionView – Stack Overflow

ios – UICollectionView – Not displaying cells – Stack Overflow

ios – Can’t get UICollectionView to display cells – Stack Overflow

UICollectionView not showing | Treehouse Community

UICollectionViewCell not rendering — Swift Coder

后来发现:

collectionView的cellForItemAtIndexPath没有执行

swift  collectionView cellForItemAtIndexPath not called

UICollectionView cellForItemAtIndexPath not called – 坤哥MartinLi – 博客园

Uicollectionview Cellforitematindexpath Not Called Fix – iOS Coder Talk

ios – collectionView:cellForItemAtIndexPath: never gets called – Stack Overflow

去掉CollectionView的registerClass:

//        self.optionListCollectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: OptionCollectionViewCellId)

结果:

问题依旧。

ios – cellForItemAtIndexPath not called but numberOfItemsInSection does – Stack Overflow

后来,把自动布局去掉,换成手动设置frame,并且把自动布局的约束去掉:

        //collection view layout

        let flowLayout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()

//        flowLayout.scrollDirection = .Horizontal

//        flowLayout.scrollDirection = .Vertical

        flowLayout.sectionInset = UIEdgeInsetsMake(5,5,5,5)

        flowLayout.minimumInteritemSpacing = 20

        flowLayout.minimumLineSpacing = 10

        flowLayout.itemSize = CGSize(width: 60, height: 40)

//        flowLayout.itemSize = CGSize(width: 30, height: 30)

//        optionListCollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)

        let collectionViewFrame = CGRectMake(0, 0, 200, 80)

        optionListCollectionView = UICollectionView(frame: collectionViewFrame, collectionViewLayout: flowLayout)

        //2. option list

        self.optionListCollectionView.delegate = self

        self.optionListCollectionView.dataSource = self

//        self.optionListCollectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: OptionCollectionViewCellId)

        //self.optionListCollectionView.backgroundColor = UIColor.clearColor()

        self.optionListCollectionView.backgroundColor = UIColor.greenColor()

        self.contentView.addSubview(self.optionListCollectionView)

//        constrain(self.optionListCollectionView, self.titleLabel) {optionListCollectionView, titleLabel in

//            optionListCollectionView.top == titleLabel.bottom + OptionOptionListPaddingTop

//            optionListCollectionView.left == titleLabel.left

//            optionListCollectionView.right == optionListCollectionView.superview!.right – OptionSubViewPaddingX

//            optionListCollectionView.bottom == optionListCollectionView.superview!.bottom – OptionOptionListPaddingBottom

//        }

然后才可以执行到cellForItemAtIndexPath

但是,手动设置frame大小的话,就没法自动适配了

导致即使手动调整了多次,但是界面还是很乱

所以希望是:

[未解决]如何让UICollectionView支持自动布局

转载请注明:在路上 » [已解决]tableview中内嵌的UICollectionView中的UICollectionViewCell不显示

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
93 queries in 0.197 seconds, using 23.38MB memory