我不明白为什么在下面的代码中,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/