【发布时间】:2022-01-23 04:19:15
【问题描述】:
我对 Swift 和编程很陌生,很抱歉这个简单的问题:
我想开发一个使用(水平滚动的)UICollectionView 作为界面的日历。 UICollectionView 的每个单元格都应该有一个带有相应日期和工作日编号的标签。
为此,我有一个 dateArray 来存储日期对象。 setupCell- 方法是将各自的数据放到UICollectionViewCell 的标签上。
显示星期日的单元格应通过与其他单元格不同的背景颜色来突出显示。
我尝试在cellForItemAt - 方法中实现此功能,但卡在那里。
我的函数如下所示:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyCollectionViewCell.identifier, for: indexPath) as! MyCollectionViewCell
let dayFormatter = DateFormatter()
let weekdayFormatter = DateFormatter()
dayFormatter.dateFormat = "dd"
weekdayFormatter.dateFormat = "EEE"
cell.setupCell(day: dayFormatter.string(from: dateArray[indexPath.item]), weekday: weekdayFormatter.string(from: dateArray[indexPath.item]))
if Calendar.current.component(.weekday, from: dateArray[indexPath.item]) == 1 {
cell.backgroundColor = UIColor.gray
}
return cell
}
使用此功能,星期日会按计划突出显示,但前提是我不滚动。在最后滚动一些单元格后,所有单元格都将突出显示。
感谢您提供解决问题的每一个提示。
【问题讨论】:
标签: swift xcode uicollectionview uicollectionviewcell