ios - swift 动态添加indexpath,数据类型出错怎么解决?
怪我咯
怪我咯 2017-04-18 09:47:06
0
2
465

创建了一个可变数组,往数组里面插入相关节点的时候indexpath

报错信息为:cannot convert value of type 'NSMutalbeArray' to expected argument type '[IndexPath]'

//获取需要修正的indexpath

let indexPathArray = NSMutableArray()
    for i in startPosition...endPosition {
        let tempIndexPath:NSIndexPath = NSIndexPath(row: i, section: 0)
        indexPathArray.add(tempIndexPath)
    }
    //插入或者删除相关节点
    if expand {
        self.insertRows(at: indexPathArray, with: UITableViewRowAnimation.none)
    }
怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(2)
Peter_Zhu

insertRows(at: _, with: _) This method, the first parameter is a [IndexPath]
Since you use swift, it is recommended to use the swift data type

This[IndexPath] 也就是 Array<IndexPath>
Array 的定义是 swift 中的一个 struct is also Array<IndexPath>
The definition of Array is a struct in swift

NSMutableArray is an objc class, two things that are completely incompatible with struct

My recommended way of writing:🎜
    var indexPathArray = [IndexPath]() // 看这里
    for i in startPosition...endPosition {
        let tempIndexPath = IndexPath(row: i, section: 0)
        indexPathArray.append(tempIndexPath) // 和这里
    }
    //插入或者删除相关节点
    if expand {
        self.insertRows(at: indexPathArray, with: .none)
    }
黄舟
let indexPathArray: [IndexPath] = []

Also, in Swift 3, the NS prefix is ​​removed, don’t use NSIndexPath,而是用 IndexPath.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template