【问题标题】:Error: Cannot assign value of type '[String]' to type 'String' in swift错误:无法将“[String]”类型的值分配给 swift 中的“String”类型
【发布时间】:2020-05-19 19:36:31
【问题描述】:

我正在尝试使用具有多个部分的 tableview 创建搜索过滤器,并在“cell?.textLabel?.text = self .searchArray[indexPath.row].rowTitles”。我该如何解决?最终目标是能够在 tableview 的各个部分中显示 rowTitles。这是我的代码。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!

    let dataArray = [(sectionTitle: "section 1", rowTitles: ["row 1", "row 2"]),
    (sectionTitle: "section 2", rowTitles: ["row 1", "row 2", "row 3"]),
    (sectionTitle: "section 3", rowTitles: ["row 1"]),
    ]
    var searchArray = [(sectionTitle: String, rowTitles: [String])]()
    var searching = false

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
    }
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searching {
            return searchArray.count
        } else {
            return dataArray.count
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell")
        if searching {
            cell?.textLabel?.text = self.searchArray[indexPath.row].rowTitles
        } else {
            cell?.textLabel?.text = self.dataArray[indexPath.row].rowTitles
        }
        return cell!
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.dataArray.count
    }
}


extension ViewController: UISearchBarDelegate {
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        searchArray =  dataArray.filter({$0.sectionTitle.lowercased().contains(searchBar.text!.lowercased())} )
        searching = true
        tableView.reloadData()
    }
}

【问题讨论】:

  • 另外你应该避免强制展开返回单元格!

标签: ios swift xcode uitableview


【解决方案1】:

首先不要使用元组作为数据源数组。使用结构。

有很多问题。基本上你根本没有考虑section 信息。您必须使用indexPath.section 获取当前部分,并使用indexPath.row 获取当前行

numberOfRowsInSection 替换为

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searching {
        return searchArray[section].rowTitles.count
    } else {
        return dataArray[section].rowTitles.count
    }
}

cellForRow

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell", for: indexPath)
    if searching {
        cell.textLabel?.text = self.searchArray[indexPath.section].rowTitles[indexPath.row]
    } else {
        cell.textLabel?.text = self.dataArray[indexPath.section].rowTitles[indexPath.row]
    }
    return cell
}

【讨论】:

    【解决方案2】:

    self.searchArray[indexPath.row].rowTitles 是字符串数组,而不是字符串。

    cell.textLabel.text 是一个字符串值,因此您不能将第一个分配给后者。

    我认为您应该使用 rowTitles 中的一个字符串并将其分配给 textLabel.text

    希望这有帮助!

    【讨论】:

      【解决方案3】:

      因为 cell?.textLabel?.text 需要 String 类型,而您将 Strings 的数组 (rowTitles) 传递给它。

      尝试从您的数组 (rowTitles) 中传递一个字符串(根据您的要求),它就会消失。

      if searching {
          cell?.textLabel?.text = self.searchArray[indexPath.row].rowTitles[0]
      } else {
          cell?.textLabel?.text = self.dataArray[indexPath.row].rowTitles.first[1]
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多