最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Swift中如何使用NSIndexPath去获得对应的index

Swift crifan 4771浏览 0评论

【背景】
之前看到已有代码:
        print("cellForRowAtIndexPath indexPath = \(indexPath)")
调试app时打印出来的log信息为:
cellForRowAtIndexPath indexPath = <NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}
cellForRowAtIndexPath indexPath = <NSIndexPath: 0xc000000000200016> {length = 2, path = 0 - 1}
cellForRowAtIndexPath indexPath = <NSIndexPath: 0xc000000000400016> {length = 2, path = 0 - 2}
cellForRowAtIndexPath indexPath = <NSIndexPath: 0xc000000000600016> {length = 2, path = 0 - 3}
如图:
NSIndexPath length and path

此处是是希望获得,对应的index而已。
但是NSIndexPath搞得好麻烦。
现在想要去搞懂,NSIndexPath,如何使用。
如何去判断是Table的哪行row的cell。
 

[折腾过程]
1.搜:
swift NSIndexPath
参考:
Comparing NSIndexPath Swift – Stack Overflow
无意中看到,原来NSIndexPath是有子项的,所以去试试:
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        var rowHeight:CGFloat = 55
       
        if indexPath.row == 3 {
            rowHeight = 20
        }
       
        return rowHeight
    }

结果是:
就可以了。



【总结】
此处,直接用函数传递进来的indexPath,然后调用row属性,即可去判断是处table的于哪一行(row)。


【后记】
去折腾:
【已解决】Swift中的NSIndexPath中到底是否有row这个属性

转载请注明:在路上 » 【已解决】Swift中如何使用NSIndexPath去获得对应的index

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.170 seconds, using 22.09MB memory