指数超出范围 - 9223372036854775807 - Swift 3

时间:2017-07-12 21:44:16

标签: indexing swift3 tableview ios10 indexoutofrangeexception

我有一个字符串数组,其中的位置显示在表中,当用户选择表格单元格时,该位置会添加到另一个数组中。我想通过第二个数组在标签中显示所选位置。但是,当我选择太多位置或位于列表中较远的位置时,我会收到此错误:

致命错误:索引超出范围

当我选择每个位置时,所有位置的索引号是9223372036854775807.如何让我的代码识别每个位置的索引号?

@IBOutlet weak var tableLocations: UITableView!
let itemsLocations: [String] = ["Pacific Northwest", "West Coast", "The West", "Midwest", "Great Lakes", "The South", "New England"]
var locationsPreviewtext = [String]()

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return itemsLocations.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellLoco: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cellLocations")!
    cellLoco.textLabel?.text = itemsLocations[indexPath.row]
    return cellLoco
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    locationsPreviewtext.append(itemsLocations[indexPath.row]) //I get the error here when I select
    labelLocationsPreview.text = "\(locationsPreviewtext)"
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    if locationsPreviewtext.isEmpty {
        return
    }
    else {
        let ind = index(ofAccessibilityElement: locationsPreviewtext[indexPath.row]) //I get the error here when I deselect
        if ind == NSNotFound {
            locationsPreviewtext.remove(at: ind)
        }
        labelLocationsPreview.text = "\(locationsPreviewtext)"
    }
}

@IBOutlet weak var labelLocationsPreview: UILabel!

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destDetails: ViewController = segue.destination as! ViewController
    destDetails.labelGifteeLocationsPreview.text = "\(locationsPreviewtext)"
}

0 个答案:

没有答案