Finding the [indexpath.row] value in a TableView

858 views Asked by At

Essentially I am attempting to change a variable when a specific row is selected however, the code is still printing -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. Would the searchBar effect my values? I first code the tableview and then the searchbar and then I implement a submit button which prints the values of my variables.

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)
    }
}

1

There are 1 answers

11
gmdev On

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. In the body of your if statements, change == to = to assign a value to the variable 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. You need to add the line to 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:

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