0

我有一个 UITableViewController。在 tableview 的代码中,如果一个值等于另一个值,我想显示一个“lineLabel”(它只是一个空白标签,在单元格底部有一个背景)——而且我的工作非常好!

问题是当我到达底部单元格时,我得到索引超出范围错误。我知道为什么会出现错误(将在代码中显示),但我不知道如何防止该错误并且仍然得到与现在相同的结果?

希望你能帮我!

这是我的代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:carTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! carTableViewCell

let checkBackgroundColor = carsArray[indexPath.row + 1].componentsSeparatedByString("#")

        if checkBackgroundColor[4] == finalCars[4] {
            cell.lineLabel.hidden = true
        } else {
            cell.lineLabel.hidden = false
        }


        return cell
    }

我得到索引超出范围错误,因为我正在检查workoutsArray[indexPath.row + 1].componentsSeparatedByString("#")我的代码部分中不存在的索引。我只是不知道该怎么做?任何人??

4

1 回答 1

2

之前检查数组计数:

if carsArray.count > indexPath.row + 1 {
    // your code here
}

但您也必须正确计算您的 tableview 行数并将其放入您的override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int

于 2016-11-02T14:42:18.213 回答