簡體   English   中英

錯誤:無法將類型“[String]”的值分配給 swift 中的“String”類型

[英]Error: Cannot assign value of type '[String]' to type 'String' in swift

我正在嘗試使用具有多個部分的表格視圖創建搜索過濾器,並在“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()
    }
}

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

cell.textLabel.text 是一個字符串值,因此您不能將第一個分配給后者。

我認為您應該使用 rowTitles 中的一個字符串並將其分配給 textLabel.text

希望這有幫助!

因為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]
}

首先不要使用元組作為數據源數組。 使用結構。

有很多問題。 基本上你根本沒有考慮section信息。 您必須使用indexPath.row獲取當前部分,使用indexPath.section獲取當前行

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM