ios - Swift 3.0 中的 NSIndexPath 和 IndexPath

标签 ios swift uicollectionview swift3

我最近将我的代码转换为 Swift 3.0。我的 Collection View 和 TableView 数据源方法现在在其方法签名中包含 IndexPath 而不是 NSIndexPath。但仍然在方法定义中,它是将 IndexPath 类型转换为 NSIndexPath。即

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell : NNAmenitiesOrFurnishingCollectionViewCell = self.amenitiesOrFurnishingCollectionView.dequeueReusableCell(withReuseIdentifier: "NNAmenitiesOrFurnishingCollectionViewCell", for: indexPath) as! NNAmenitiesOrFurnishingCollectionViewCell
        cell.facilityImageName = self.facilityArray[(indexPath as NSIndexPath).row].imageName
        cell.facilityLabelString = self.facilityArray[(indexPath as NSIndexPath).row].labelText
        return cell
    }

谁能告诉我为什么 indexPath 被类型转换为 NSIndexPath

最佳答案

这里没有理由将 IndexPath 转换为 NSIndexPath。 Swift 3 覆盖类型 IndexPath 具有属性

/// The section of this index path, when used with `UITableView`.
///
/// - precondition: The index path must have exactly two elements.
public var section: Int

/// The row of this index path, when used with `UITableView`.
///
/// - precondition: The index path must have exactly two elements.
public var row: Int

您可以直接访问:

    cell.facilityImageName = self.facilityArray[indexPath.row].imageName
    cell.facilityLabelString = self.facilityArray[indexPath.row].labelText

显然,Xcode 迁移器的工作并不完美。

关于ios - Swift 3.0 中的 NSIndexPath 和 IndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39631905/

相关文章:

ios - 如何确定刚刚在 Collection View 中编辑了哪个单元格的文本字段?

ios - 在 iOS 中停止 View 旋转

ios - 如何在UserDefaults中保存Double数组?

ios - 为 watchOS 2.0 实现核心数据

在 Swift 中对包含更复杂值的字典进行排序

swift - RxSwift,删除带有动画的单元格

iphone - UICollectionView 单元格重叠

ios - 谷歌自动完成教程 places api for swift

ios - 在 UITextView 中选择一个词

ios - 如何从 Collection View 单元格内调用 Collection View Controller