如何访问NSIndexPath节值?

Pas*_*Kit 4 nsindexpath ios swift

我无法理解为什么在以下代码indexPath.section中重新调整null值.

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)
Run Code Online (Sandbox Code Playgroud)

这个问题之后,我也尝试了以下方法来访问section值:

if let section = indexPath?.section {
    NSLog("Index Path Section %@", section)
}
Run Code Online (Sandbox Code Playgroud)

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

我不是想做任何复杂的事情,我只需要访问section值,这样我就可以有条件地返回正确类型的单元格.

Bry*_*hen 6

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

section具有的类型NSInteger,这是Int在迅速

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

它打印(null)是因为节值是0.并且1因为它不是有效的对象指针而崩溃