arrays - 从 collectionView 中取消选择并从数组中取消项目

标签 arrays swift uicollectionview

我有以下代码:

 var selectedIndexPaths = [NSDate]()

    func collectionView(collectionView: UICollectionView,  didSelectItemAtIndexPath indexPath: NSIndexPath) {

        let cell = collectionView.cellForItemAtIndexPath(indexPath) as?  PartExpense

        if cell?.selected == true {
            cell?.layer.borderWidth = 4.0
            cell?.layer.borderColor = UIColor.greenColor().CGColor

            selectedIndexPaths.append(cell!.idPartecipant)

            print(selectedIndexPaths)


        }
    }

    func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.cellForItemAtIndexPath(indexPath)
        if cell?.selected == false {
            cell?.layer.borderColor = UIColor.clearColor().CGColor


            if let index = selectedIndexPaths.indexOf(indexPath.row) {
                selectedIndexPaths.removeAtIndex(index)
                print(selectedIndexPaths)
            }

        }

    }

一切顺利,直到行: 如果让index = selectedIndexPaths.indexOf(indexPath.row)

这是错误:“无法将 Int 类型的值转换为预期的 NSDate 类型”我该如何实现此目的?如何转换数组以避免此错误?

最佳答案

您编写的方式是询问该数组中的所有 NSDate 日期“indexPath.row”的索引是什么。错误是说indexPath.row是一个int而不是一个日期。您到底想做什么?

关于arrays - 从 collectionView 中取消选择并从数组中取消项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36319107/

相关文章:

c - 在 C 中将数据复制到空字符串时出现意外截断

swift - 在 swift VC 代码中以编程方式指定约束时,布局属性配对无效

ios - 在 UISearchController 中实现 updateSearchResultsForSearchController 时遇到问题?

swift - 在方法参数中隐式解包可选闭包

ios - 首次加载时 UICollectionViewCell 内容大小错误

ios - 为什么 Collection View 中的选定单元格在 UICollection View Swift 3 中无法正确显示?

ios - 类型转换数组的最有效方法

javascript - 奇怪的 ES6 Array.every 行为

arrays - 索引第二个数组元素始终返回零

swift - 单元格仅显示数组中的最后一个字符串