ios - 如何更改屏幕上尚未显示的单元格的属性?

标签 ios swift uicollectionview

我有一个由时间表 (UITableView) 和日间栏 (UICollectionView) 组成的应用程序。用户可以通过以下两种方法之一更改他们选择的日期:

  1. 从 DayBar 中选择一天
  2. 点击表格 View 底部的“JumpToTomorrow”按钮。

2. 现在的问题是,当我在第二天尚未滚动到屏幕上的情况下单击它时,当天的单元格未被选中(见下文)。 enter image description here

DayCollectionViewCell.isSelected(由于某种原因未被调用)

    override var isSelected: Bool {
    didSet {
        self.backgroundColor        = self.getBackgroundColor(selected: isSelected)
        self.layer.borderColor      = self.getBorderColor(selected: isSelected)
        self.number.attributedText  = NSAttributedString(string: number.text!, attributes: self.getTextAttributes(selected: isSelected))
    }
}

DayBarController 函数

    override func viewDidLoad() {
    super.viewDidLoad()
    // To preserve selection between presentations
    self.clearsSelectionOnViewWillAppear = false
    setEdgeInsets()
    BroadcastManager.listen(to: PrepNotifications.JumpToTomorrowClicked, listenerId: "UpdateSelectedDay", react: updateSelectedDay)
}
    /// change the selected day and notify the app about the change
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    PrepGlobals.Calendar.SELECTED_DAY = indexPath.row
    BroadcastManager.broadcast(PrepNotifications.SelectedDayChanged)
    //scroll to center on selected day in case of clicked on today  
    if (PrepGlobals.Calendar.TODAY_INDEX == indexPath.row){
        self.collectionView?.scrollToItem(
            at: indexPath,
            at: .centeredHorizontally,
            animated: true
        )
    }
}

private func updateSelectedDay(_ userInfo: [AnyHashable: Any]? = nil) {
    PrepGlobals.Calendar.SELECTED_DAY += 1
    let selectedDayIndex = IndexPath(row: PrepGlobals.Calendar.SELECTED_DAY, section: 0)
    self.collectionView!.selectItem(at: selectedDayIndex, animated: true, scrollPosition: .right)
    BroadcastManager.broadcast(PrepNotifications.SelectedDayChanged)
}

最佳答案

我编辑了我的答案。实际上,您必须手动设置 isSelected。

override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
      cell.isSelected = (indexPath.row == PrepGlobals.Calendar.SELECTED_DAY)
}

关于ios - 如何更改屏幕上尚未显示的单元格的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44302760/

相关文章:

ios - UITableView 的 LikeButton

swift - 等效的 Swift NSURLSession 或 Alamofire Curl 帖子

iOS音频系统。开始&停止还是只是开始?

javascript - JSON 应用程序未响应网络代码

xcode - 在 for 循环内附加图像数组 - swift

ios - UICollectionViewScrollPosition 不工作

ios - UICollectionViewDropDelegate 破坏了 UICollectionView 中的重新排序单元机制

ios - UICollectionView ScrollView 从顶部和底部有边距

ios - 如何强制应用程序目标在 IOS 中使用一种语言?

ios - 切换选项卡后 UIViewcontroller 消失 UITabBarController