我有一个由时间表 (UITableView
) 和日间栏 (UICollectionView
) 组成的应用程序。用户可以通过以下两种方法之一更改他们选择的日期:
- 从 DayBar 中选择一天
- 点击表格 View 底部的“JumpToTomorrow”按钮。
2. 现在的问题是,当我在第二天尚未滚动到屏幕上的情况下单击它时,当天的单元格未被选中(见下文)。
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/