【问题标题】:Finding the [indexpath.row] value in a TableView在 TableView 中查找 [indexpath.row] 值
【发布时间】:2020-09-28 17:09:28
【问题描述】:

基本上我试图在选择特定行时更改变量,但是代码仍在打印-1。这是我所有相关的代码。我试图能够单击某个表格视图单元格,然后能够打印出该文本。 searchBar 会影响我的价值观吗?我首先对 tableview 进行编码,然后对 searchbar 进行编码,然后实现一个提交按钮,用于打印变量的值。

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

【问题讨论】:

  • 你需要放全表代码。现在,你展示的还不够。

标签: ios swift xcode tableview indexpath


【解决方案1】:

有一些问题无法实现您想要的:

== 运算符正在检查两个变量是否相等,而不是将一个变量分配给另一个变量,它将返回一个布尔值,truefalse。在 if 语句的主体中,将 == 更改为 = 以将值分配给变量 num。

将您的代码更改为:

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

在看到您更新的代码后,您似乎只设置了 tableView 的 dataSource 而不是委托。您需要将行添加到viewDidLoad

tableView.delegate = self

此外,您可以用一行替换整个代码体,而不是使用多个 if 语句来检查 indexPath

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

【讨论】:

  • 我尝试了你的答案,但我收到的值仍然是 -1
  • 如何以及在哪里打印 num 的值?您是否也在其他地方更改 num 的值?
  • 嘿@Mark,看看我的更新答案 - 我相信我找到了问题的根源。
  • 我没有改数字,按一下按钮就会打印数字
  • 不幸的是,我已经尝试了你给我的东西,但还是不行,非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-20
相关资源
最近更新 更多