【发布时间】:2015-08-19 02:38:39
【问题描述】:
indexPath.row 在 iPhone 6 上的计算不正确,但在任何其他模拟器上都没有,如下所示。 iOS 部署目标设置为 8.0。当您从(并且仅从)第 4 个选项卡单击分段控件中的第 2 个选项卡时,会发生此计算。
这是应用崩溃的地方:
func createCompletedCell(indexPath: NSIndexPath) -> CompletedCell {
var cell = tableView.dequeueReusableCellWithIdentifier(profileCompletionId) as! CompletedCell
println("indexPath.row: \(indexPath.row)")
println("indexPath.row-2: \(indexPath.row-2)")
var completion = switchState == 1 ? completionArr[indexPath.row-2] : favArr[indexPath.row-2] //crashes on this line
cell.setCompletedCell(completion)
return cell
}
调用自:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
var cell = tableView.dequeueReusableCellWithIdentifier("ProfileDetails", forIndexPath: indexPath) as! ProfileDetails
cell.selectionStyle = UITableViewCellSelectionStyle.None //No background color if cell is clicked
return cell
}
else if indexPath.row == 1 {
var cell = tableView.dequeueReusableCellWithIdentifier("SegControl", forIndexPath: indexPath) as! ProfileSegControl
cell.segControl.selectedSegmentIndex = switchState
cell.segControl.addTarget(self, action: Selector("changeState:"), forControlEvents: UIControlEvents.ValueChanged)
return cell
}
else {
if(switchState == 0 || switchState == 2){
return createBountyCell(indexPath)
}
else {
return createCompletedCell(indexPath)
}
}
}
当我在 iPhone 6 模拟器上打印 indexPath.row 变量时,我点击第 4 个分段控制选项卡,然后点击第 2 个,我得到输出:
(4th tab calcs)
indexPath.row: 2
indexPath.row-2: 0
indexPath.row: 3
indexPath.row-2: 1
(2nd tab calcs)
indexPath.row: 3
indexPath.row-2: 1
当我用 iPhone 4s/5/5s/6plus 做同样的事情时
(4th tab calcs)
indexPath.row: 2
indexPath.row-2: 0
indexPath.row: 3
indexPath.row-2: 1
(2nd tab calcs)
indexPath.row: 2
indexPath.row-2: 0
问题是,当我单击第二个选项卡时,indexPath.row 应该设置为 2,但改为设置为 3。
【问题讨论】:
-
你看过你的故事板了吗?
-
是的,但看起来没什么异常。我到底应该看什么? @ILikeTau
-
表格视图。或者,您可以尝试将您的更改与 git 同步并重试吗? (记得在模拟器中重置内容和设置,这样一切都消失了)。
-
@ILikeTau 看了看,一切正常。我们确实注意到了,但是我们都使用了不同的版本,这就是问题所在。然而,我们仍然不确定为什么会发生这种情况。
-
好的,所以没有问题。在
cellForRowAtIndexPath中,未“计算”索引路径。就是这样。它是表格当时恰好需要的单元格的索引路径。你不应该对此做任何假设:你应该只提供进入它所在行的单元格。
标签: ios xcode swift nsindexpath