ios - 如果 selectItem 靠近当前选定的单元格,则不选择单元格

标签 ios swift uicollectionview

我目前正在开发一个 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/

相关文章:

swift - Codable 与 NSOrdered 集?

swift - 无法导入桥接头文件 - Swift

ios - 中心 UICollectionViewCells

ios - Swift 3 - 滑动堆栈 View ?

ios - 1 个屏幕包含 2 个 View Controller 的最佳方法

ios - 来自 iOS 应用程序的 "Timed out trying to acquire capabilities data"是什么意思?

ios - Firebase 观察者事件类型

swift - 缺少 RealmSwift.framework

iphone - 自动布局 Collection View

IOS/objective-C : Reduce file size of pngs for thumbnails