从UITableView获取textLabel

时间:2019-06-07 11:03:00

标签: swift uitableview

我需要从textLabel中的行中获取UITableView。但是我得到nil。我需要获取textLabel,因为我正在使用UISearchBar,如果我尝试使用索引获取数据,那么在搜索时,我将收到不正确的索引。因此,我想获得textLabel。 请修复我错的地方

当我在searchBar中输入TableView项时,更改。但是索引没有。例如Food = ["Apple", "Banana", "Coca-Cola"]。如果我使用searchBar并输入“香蕉”。然后,我单击它,但得到了Apple(因为它是索引-0)。这就是为什么我想得到textLabel

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //let meal = foods[indexPath.item]
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        let labelContent = cell.textLabel!.text
        print(labelContent)
    }

UISearchBar

extension FoodViewController: UISearchBarDelegate {

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    searchedFoods = foods.filter({ $0.title.lowercased().prefix(searchText.count) == searchText.lowercased() })
    searching = true
    tableView.reloadData()
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searching = false
    searchBar.text = ""
    tableView.reloadData()
  }

}

6 个答案:

答案 0 :(得分:3)

永远不要从单元格视图获取数据,总是从数据源模型获取数据。

并且从不dequeueReusableCell之外使用cellForRowAt。您将无法获得所需的电池。

在这种情况下,您必须根据searching

获取数据
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let labelContent : String
    if searching { 
       labelContent = searchedFoods[indexPath.row].title
    } else {
       labelContent = foods[indexPath.row].title
    }
    print(labelContent)
}

您的filter方法太可怕了。将其更改为更有效的

searchedFoods = foods.filter { $0.title.range(of: searchText, options: [.caseInsensitive, .anchored] != nil) }

最后,我建议更改textDidChange以在搜索文本为空时也取消搜索,并从searchedFoods中删除未使用的项目。

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    if searchText.isEmpty {
        searchedFoods.removeAll()
        searching = false
    } else {
        searchedFoods = foods.filter{ $0.title.range(of: searchText, options: [.caseInsensitive, .anchored]) != nil }
        searching = true
    }
    tableView.reloadData()
}

答案 1 :(得分:1)

我发现问题在于您使用的是indexPath.item,这就是为什么您获得错误的索引的原因,请尝试

let meal = foods[indexPath.row]

答案 2 :(得分:0)

如果您想在UILabel上点击cell来获取文本

确认UITableViewDelegate

//Access the array that you have used to fill the tableViewCell
    print(foods[indexPath.row]) it will print selected row 
OR

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath) as! UITableViewCell
        let labelContent = cell.textLabel!.text
        print(labelContent)
    }

答案 3 :(得分:0)

让我们说而不是从食物中获取数据(在didSelect方法中)是从搜索到的食物中获取数据。但是,要使其正常工作,您应该首先为食物分配食物,并且当用户清除搜索栏时,应再次将食物重新设置为食物数据。

答案 4 :(得分:0)

请尝试这样

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if searching {
            print(searchedFoods[indexPath.row])
        } else {
            print(foods[indexPath.row])
        }
    }

答案 5 :(得分:0)

尝试一下

扩展名FoodViewController:UITableViewDataSource {

func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)-> UITableViewCell {         let cell = tableView.dequeueReusableCell(withIdentifier:“ Cell”,for:indexPath)

    if searching {
        let food = searchedFoods[indexPath.row]
        cell.textLabel?.text = food.title
    } else {
        let food = foods[indexPath.row]
        cell.textLabel?.text = food.title
    }
    return cell


}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searching {
        return searchedFoods.count
    } else {
        return foods.count
    }
}

func tableView(_ tableView:UITableView,didSelectRowAt indexPath:IndexPath){

如果搜索{

         let meal = searchedFoods[indexPath.row]
    let labelContent = meal
    print(labelContent)
    } else {
         let meal = foods[indexPath.row]
    let labelContent = meal
    print(labelContent)
    }

}
相关问题