我最近将我的代码转换为 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/