首页 > 解决方案 > 如何通过访问它们的 isHidden 属性来显示多个 UIView?

问题描述

我想根据下面的逻辑语句在集合单元格中显示多个 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,2 个默认,1 个假期和 1 个生日。生日和节日活动在同一天,因此两者都birthdayBar应该holidayBar在当天可见。我还希望看到defaultBar其他两个事件的 a,但是当我运行应用程序时,只有 holidayBar 可见。

标签: swiftuicollectionview

解决方案


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


推荐阅读