我目前正在开发一个 UIViewController,上面有一个水平 collectionView,上面有接下来几个月的星期几(比如日历),下面我有一个 tableView,其中有在用户位置附近发生的下一个事件. 当用户向下滚动表格 View 时,它会获取顶部单元格事件日期并将 collectionView 滚动到那一天。我正在使用以下代码:
在 tableView 的 cellForRowAt 上:
datePicker.selectItem(at: selectedIndexPath, animated: true, scrollPosition:UICollectionViewScrollPosition.centeredHorizontally)
在 collectionView 的 cellForItemAt 上:
if cell.isSelected {
cell.selectedCircle.layer.cornerRadius = 15
cell.selectedCircle.backgroundColor = UIColor(red:1, green:0.08, blue:0.35, alpha:1)
cell.weekDayLabel.alpha = 1
selectedMonth.text = dateToday.3.firstUppercased
} else {
cell.selectedCircle.backgroundColor = UIColor(red:1, green:0.08, blue:0.35, alpha:0)
cell.weekDayLabel.alpha = 0.4
}
当要选择的下一个 collectionView IndexPath 与前一个相距甚远时,一切正常。当它们相距大约 8 个或低于 indexPaths 时,collectionView 滚动到正确的 indexPath 但不运行 isSelected 代码。
有什么想法吗?感谢您的时间。
最佳答案
看起来您正在单元格中存储 isSelected 信息。不。您可能会得到一个以前在其他地方回收的电池。将选择哪些单元格的逻辑保留在其他东西(如数组)中,并根据索引路径查找它。
关于ios - 如果 selectItem 靠近当前选定的单元格,则不选择单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50509984/