array.index(of :)无法正常工作

时间:2017-09-16 23:56:03

标签: ios arrays swift uitableview indexing

我正在尝试在数组中找到UITableViewCell文本。问题是无论我点击什么单元格,array.index(of:)总是返回1.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell =  tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    if let text = cell.textLabel?.text {
        let indexOfCellString = event.index(of: text)
        print (indexOfCellString!)
    }

}
//here is my attempt to set the cell text:
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

    let cell =  tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    if eventIndex.indices.contains(indexPath.row) == true {
    cell.textLabel?.text = event [indexPath.row]
    }

    return cell
}

event是我正在尝试搜索并找到单元格文本的数组。 并且单元格的文本始终位于事件索引中。因为我的tableview从事件数组中读取信息。因此它不应该总是返回1。 我做错了什么?

1 个答案:

答案 0 :(得分:1)

切勿在{{1​​}}之外使用dequeueReusableCell。只需从您的数据模型中访问您的数据。

cellForRowAt
相关问题