ios - 如何访问 NSIndexPath 部分值?

标签 ios swift nsindexpath

我不明白为什么在下面的代码中,indexPath.section 正在重新调整一个空值。

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell? {
    NSLog("Index Path %@", indexPath)
    // Index Path <NSIndexPath: 0x1666d1d0> {length = 2, path = 0 - 0}

    NSLog("Index Path Section %@", indexPath.section)
    // Index Path Section (null)

正在关注 this question我还尝试了以下方法来访问部分值:

if let section = indexPath?.section {
    NSLog("Index Path Section %@", section)
}

我有一个包含 2 个部分的表格,每个部分有 1 行。在 path = 0 - 0 的第一次迭代中,日志显示 Index Path Section (null)。在 path = 1 - 0 的第二次迭代中,程序崩溃。

我不想做任何复杂的事情,我只需要访问节值,以便我可以有条件地返回正确类型的单元格。

最佳答案

您正在正确访问节值。您只是以错误的方式打印它。

section的类型是NSInteger,在swift中是Int

只需将日志更改为 NSLog("Index Path Section %d", indexPath.section)

它打印(null) 因为section 值为0。当它是 1 时崩溃,因为它不是有效的对象指针

关于ios - 如何访问 NSIndexPath 部分值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24155397/

相关文章:

ios - Swift View Controller 初始化程序问题

iphone - 为什么我的文件没有保存?

cocoa-touch - iOS indexPath 为零(自定义附件图像)

ios - 以圆形模式向外移动 SpriteNode

swift - 通过 getDataInBackgroundWithBlock 检索 PFFile 时出错

ios - swift : Cast from NSArray to unrelated type NSIndexPath Always fails

iphone - 在 iPhone 的 Objective-C 中的 (commitEditingStyle) 之外调用 (deleteRowsAtIndexPaths)

ios - 在 swift 中将 JSON 字符串数组转换为 NSArray

ios - 动画 subview

快速转换为具有约束的通用类型