我有一個應用程序,它包含一個時間表(UITableView
)和一個白天(UICollectionView
)。用戶可以通過以下兩種方法之一改變自己選定日期:如何更改尚未顯示在屏幕上的單元格的屬性?
- 從DayBar
- 選擇一天點擊「JumpToTomorrow」按鈕,表格視圖的底部。
現在的問題是,當我在第二天尚未滾動到屏幕上的場景中單擊它時,未選擇當天的單元格(請參閱下文)。
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)
}
這也適用。 – Deco