Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 切换到其他页面时,所选单元格将被取消选择_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios 切换到其他页面时,所选单元格将被取消选择

Ios 切换到其他页面时,所选单元格将被取消选择,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我使用UICollectionView制作自定义日历。从日历中,当我选择一些日期并向前移动到下个月,然后再返回到上个月时,所选项目被取消选择。也许这种情况发生在可重复使用的细胞上。我怎样才能解决这个问题。 为了更好地了解我想要什么: 从九月我选择4,5,然后移动到八月/七月/十一月(在本月可能选择其他日期或不选择其他日期) 然后返回到九月。在九月我想显示4,5为选中状态 我使用didSelectItemAtindexPath尝试了这一点,但是当返回到septer时,所选项目被取消选择 publi

我使用
UICollectionView
制作自定义日历。从日历中,当我选择一些日期并向前移动到下个月,然后再返回到上个月时,所选项目被取消选择。也许这种情况发生在可重复使用的细胞上。我怎样才能解决这个问题。 为了更好地了解我想要什么:

  • 九月
    我选择4,5,然后移动到
    八月/七月/十一月
    (在本月可能选择其他日期或不选择其他日期)
  • 然后返回到九月。在
    九月
    我想显示4,5为选中状态
  • 我使用
    didSelectItemAt
    indexPath尝试了这一点,但是当返回到
    septer
    时,所选项目被取消选择

    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            if let cell = collectionView.cellForItem(at: indexPath) as? CalendarDateRangePickerCell {
                if cell.isSelected == true {
                    cell.backgroundColor = .blue
                    cell.label.textColor = .white
                    cell.isUserInteractionEnabled = true
                }
                selectedDate = cell.date!
            }
    
        }
    

    当您滚动时,
    UITableView
    UICollectionView
    中的单元格会重复使用,这就是为什么您应该将所选日期存储在其他位置的原因。然后,在
    cellForItem
    中,您应该设置
    isSelected

    首先创建所选单元格的数组。如果使用模型将数据设置为单元格,则可以创建选定模型的数组。也可以创建选定行的数组

    假设您正在使用一个模型

    var selectedDates: [DateModel] = []
    
    然后

    然后在您的cellForItem中

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      if selectedDates.contains(dataSourceModel[indexPath.row]) {
         cell.isSelected = true
      }
    }
    
    另外,请确保取消选中时删除模型

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
            if selectedDates.contains(dataSourceModel[indexPath.row]) {
              selectedDates.remove(dataSourceModel[indexPath.row])
            }
        }
    

    *可能包含一些语法错误,但您可以按照此路径到达所需位置。

    您是否尝试过在
    viewDidLoad()中将
    clearselectiononviewwillapper
    设置为
    false

    一个布尔值,指示控制器是否在集合视图出现时清除选择

    此属性的默认值为true。如果为true,则集合视图控制器在收到ViewWillDisplay(:)消息时会清除集合视图的当前选择。将此属性设置为false将保留选择

    资料来源:

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
            if selectedDates.contains(dataSourceModel[indexPath.row]) {
              selectedDates.remove(dataSourceModel[indexPath.row])
            }
        }