使用swift的具有特定列数和行数的UICollectionview

时间:2018-01-17 08:40:39

标签: ios swift3 uicollectionview multiple-columns rows

我想使用 Swift3 来实现以下功能,但我不知道这样做的最佳方法。

enter image description here

我尝试了集合视图,但无法弄清楚如何在collectionView中使用特定数量的列和行。我尝试将customLayout与collectionView一起使用,但没有到达任何地方。

请帮忙,谢谢

这里我的代码结果的最后一个版本是所有项目都显示在一行中,集合视图水平滚动。 另外,我的collectionView已经在tableviewCell中。

extension QuestionsHomeVC: UICollectionViewDelegate, UICollectionViewDataSource {
   func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    let itemIndex = collectionView.tag;
    let answers = pageData.questionItems[itemIndex].answers;
    let coloums = answers.filter{$0["questionDetailType"] as! Int == 2 };
    return coloums.count + 1
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
    let itemIndex = collectionView.tag;
    let answers = pageData.questionItems[itemIndex].answers;
    let rows = answers.filter{$0["questionDetailType"] as! Int == 1 };
    return rows.count + 1
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if(indexPath.row == 0 && indexPath.section == 0){
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HeaderMatrixCell", for: indexPath) as! HeaderMatrixCell
        cell.lbl.text = "";
        return cell;
    }else{
        if(indexPath.section == 0){
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HeaderMatrixCell", for: indexPath) as! HeaderMatrixCell
            let itemIndex = collectionView.tag;
            let answers = pageData.questionItems[itemIndex].answers;
            let coloums = answers.filter{$0["questionDetailType"] as! Int == 2 };
            if !(coloums[indexPath.row - 1]["title"] is NSNull) {
                cell.lbl.text = coloums[indexPath.row - 1]["title"] as! String?
            }
            cell.backgroundColor = .lightGray;
            return cell;
        }else{
            if(indexPath.row == 0){
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HeaderMatrixCell", for: indexPath) as! HeaderMatrixCell
                let itemIndex = collectionView.tag;
                let answers = pageData.questionItems[itemIndex].answers;
                let rows = answers.filter{$0["questionDetailType"] as! Int == 1 };
                if !(rows[indexPath.row]["title"] is NSNull) {
                    cell.lbl.text = rows[indexPath.row]["title"] as! String?
                }
                cell.backgroundColor = .lightGray;
                return cell;
            }else{
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BodyItemMatrixCell", for: indexPath) as! BodyItemMatrixCell
                return cell;
            }
        }
    }
}}

0 个答案:

没有答案