swift UICollectionViewCell文本没有出现在带有两个自定义单元格的UICollectionView中

时间:2016-08-04 07:27:36

标签: swift uicollectionview uicollectionviewcell

我正在尝试使用UICollectionView制作时间表应用。 所以我制作了两个自定义单元类,TimeTableCell和TimeTableDescriptionCell。 TimeTableCell有两个标签来显示主题名称和课堂。 TiemTableDescriptionCell用于第一部分和第一行,用于显示星期几和时间。 我只是简单地在故事板中拖动UICollectionView中的UICollectionViewCell,设计了两个单元格,并用TimeTableCell和TimeTableDescriptionCell连接它们。 这两个单元格的标识符不同。

这是我的collectionView(collectionView:,cellForItemAtIndexPath:)

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    print("\(indexPath.section), \(indexPath.row)")
    let dayArray = ["", "월", "화", "수", "목", "금"]
    let cell: UICollectionViewCell!
    if indexPath.section == 0 {
        // Day
        cell = collectionView.dequeueReusableCellWithReuseIdentifier("TimeTableDescriptionCell", forIndexPath: indexPath) as! TimeTableDescriptionCell
        cell.contentView.layer.borderWidth = 1
        cell.contentView.layer.borderColor = UIColor.blackColor().CGColor
        (cell as! TimeTableDescriptionCell).titleLabel!.text = "월"
        return cell
    } else if indexPath.row == 0 {
        // index
        cell = collectionView.dequeueReusableCellWithReuseIdentifier("TimeTableDescriptionCell", forIndexPath: indexPath) as! TimeTableDescriptionCell
        cell.contentView.layer.borderWidth = 1
        cell.contentView.layer.borderColor = UIColor.blackColor().CGColor
        (cell as! TimeTableDescriptionCell).titleLabel.text = "1"
        return cell
    } else {
        cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! TimeTableCell
        if let timeTableElement = self.timeTable[indexPath.section-1][indexPath.row-1] {
            (cell as! TimeTableCell).subjectNameLabel.text = timeTableElement.subjectName
            (cell as! TimeTableCell).subjectNameLabel.adjustsFontSizeToFitWidth = true
            (cell as! TimeTableCell).classRoomLabel.text = timeTableElement.classRoom
            (cell as! TimeTableCell).classRoomLabel.adjustsFontSizeToFitWidth = true
        } else {
            (cell as! TimeTableCell).subjectNameLabel.text = ""
            (cell as! TimeTableCell).classRoomLabel.text = ""
        }
        cell.contentView.layer.borderWidth = 1
        cell.contentView.layer.borderColor = UIColor.blackColor().CGColor
        print("class")
        return cell
    }
    return cell
}

这是我的故事板。

my storyboard

当我调试时,TimeTableCells表现得很好。 但是TimeTableDescriptionCells并没有像这样出现。

debugg

在第一部分和第一行中,应该有星期几和时间。 我该如何解决这个问题?

编辑:

我的其他UICollectionViewDataSource函数是

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    if timeTableData == nil {
        return 0
    }
    return self.timeTable.count+1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if timeTableData == nil {
        return 0
    }
    return self.timeTable[0].count+1
}

func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
    if indexPath.section == 0 {
    }
}

我不想用“星期一”,“星期二”填充第一部分......和1,2,3,4的第一行......

0 个答案:

没有答案