简体   繁体   English

在 TableView 中查找 [indexpath.row] 值

[英]Finding the [indexpath.row] value in a TableView

Essentially I am attempting to change a variable when a specific row is selected however, the code is still printing -1.本质上,我试图在选择特定行时更改变量,但是代码仍在打印 -1。 Here is all my code relating.这是我所有相关的代码。 I am trying to be able to click a certain tableview cell and then be able to print out that text.我试图能够单击某个 tableview 单元格,然后能够打印出该文本。 Would the searchBar effect my values? searchBar 会影响我的价值观吗? I first code the tableview and then the searchbar and then I implement a submit button which prints the values of my variables.我首先对 tableview 进行编码,然后对搜索栏进行编码,然后我实现了一个提交按钮,该按钮打印我的变量的值。

class ViewController: UIViewController, UITableViewDataSource, UISearchBarDelegate, UITableViewDelegate {
    
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!
    
    let Data = ["dog","cat","goat"]
    
    var filteredData: [String]!
    
    var num = -1
    
    var animal: String = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if tableView != nil {
            self.tableView.dataSource = self
        }
        filteredData = Data
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
        cell.textLabel?.text = filteredData[indexPath.row]
        print(indexPath.row)
        return cell
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return filteredData.count
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            num = 0
        }
        if indexPath.row == 1 {
            num =  1
        }
        if indexPath.row == 2 {
            num = 2
        }
    }
    
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        filteredData = searchText.isEmpty ? Data : Data.filter { (item: String) -> Bool in
            // If dataItem matches the searchText, return true to include it
            return item.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil
        }
        tableView.reloadData()
    }
    
    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        self.searchBar.showsCancelButton = true
    }
    
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchBar.showsCancelButton = false
        searchBar.text = ""
        searchBar.resignFirstResponder()
    }
    
    @IBAction func Submit(_ sender: Any) {
        print(num)
        print(filteredData.count)
        if num == 0 {
            animal = "dog"
        }
        if num == 1 {
            animal = "cat"
        }
        if num == 2 {
            animal = "goat"
        }
        print(animal)
    }
}

There are a few issues that are not allowing you to achieve what you want:有一些问题无法让您实现您想要的:

The == operator is checking if two variables are equal to each other, not assigning one variable to another, and it will return a boolean value, either true or false . ==运算符检查两个变量是否彼此相等,而不是将一个变量分配给另一个变量,它将返回一个布尔值, truefalse In the body of your if statements, change == to = to assign a value to the variable num.if语句的主体中,将==更改为=以将值分配给变量 num。

Change your code to:将您的代码更改为:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.row == 0 {
        num = 0
    }
    if indexPath.row == 1 {
        num =  1
    }
    if indexPath.row == 2 {
        num = 2
    }
}

After seeing your updated code, it looks like you only set the dataSource of the tableView and not the delegate.看到您更新的代码后,您似乎只设置了 tableView 的dataSource而不是委托。 You need to add the line to viewDidLoad :您需要将行添加到viewDidLoad

tableView.delegate = self

Also, instead of having multiple if statements to check the indexPath , you could replace that entire body of code with one line:此外,您可以用一行替换整个代码体,而不是使用多个if语句来检查indexPath

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    num = indexPath.row
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM