如何通过访问其isHidden属性显示多个UIView?

时间:2019-05-30 22:34:59

标签: swift uicollectionview

我想根据以下逻辑语句在一个收集单元中显示多个UIView。

此功能用于我的标签栏应用程序中的日历视图。我正在从Firebase Firestore上的数据库中读取事件,并将其保存在calData中。

func configureEventDotFor(cell: CalendarCell, cellState: CellState) {
    let dateString = self.globalFormatter.string(from: cellState.date)
        for event in self.calData.events! {
        let eventDateString = self.globalFormatter.string(from: event.startDate)
        if dateString != eventDateString {
            cell.holidayBar.isHidden = true
            cell.birthdayBar.isHidden = true
            cell.defaultBar.isHidden = true
        } else if event.category == "birthday" {
                cell.birthdayBar.isHidden = false
        } else if event.category == "holiday" {
                cell.holidayBar.isHidden = false
        } else if event.category == "default" {
            cell.defaultBar.isHidden = false
        } else {
            return
        }
    }
}

calData中有四个事件,两个默认事件,一个假期和一个生日。生日和节日活动在同一天,因此birthdayBarholidayBar在该天都应该可见。我还希望看到其他两个事件的defaultBar,但是在运行应用程序时,只有holidayBar可见。

1 个答案:

答案 0 :(得分:0)

问题是您似乎试图直接弄乱UICollectionView单元格的子视图。不要那样做只需根据一些易于访问的状态(通常是数据模型的内容),在cellForItemAt方法中设置每个子视图的显示/隐藏状态。如果状态发生变化,只需重新加载集合视图即可。