简体   繁体   English

Swift:如何访问选定表视图单元格的值

[英]Swift: How to access value of selected table view cells

How can I access the value of the cell of the table view that the user selects?如何访问用户选择的表格视图的单元格的值? I know how to access the value if the user hasn't searched because I can just access the IndexPath's value of my array, but I can't when the user has searched something because the townArray won't line up with the cells that are shown.如果用户没有搜索,我知道如何访问该值,因为我可以访问我的数组的 IndexPath 值,但是当用户搜索某些内容时我不能,因为 townArray 不会与单元格对齐显示。

To give you a better understanding- just say I have an array of fruits which has [apples, bananas, oranges].为了让您更好地理解 - 只需说我有一系列水果,其中有 [苹果、香蕉、橙子]。 If the user searches for bananas, then the only cell with text showing (results) will say bananas.如果用户搜索香蕉,那么唯一显示(结果)文本的单元格将显示香蕉。 If I then try to access the IndexPath element of fruits, I will get apples since it is the first element of fruits and bananas is the first element showing.如果我然后尝试访问水果的 IndexPath 元素,我会得到苹果,因为它是水果的第一个元素,而香蕉是显示的第一个元素。 What I want is to get the access the value bananas when the user selects bananas and searches "bananas" instead of apples.我想要的是当用户选择香蕉并搜索“香蕉”而不是苹果时访问值香蕉。 I know this may be confusing but please let me know if you have any thoughts on how I can solve this issue.我知道这可能会令人困惑,但如果您对如何解决此问题有任何想法,请告诉我。

var searchedResult: [Fruit] = yourSearchingFunction()
tableview.reloadData()

IndexPaths would refresh correctly after TableView reloaded with a new collection of fruits在 TableView 重新加载新的水果集合后,IndexPaths 将正确刷新

Explanation is showed with the following code:使用以下代码进行说明:

class ViewController: UIViewController {

    @IBOutlet private weak var tableView: UITableView!
    private var originalFruits: [Fruit] = [] // This data is used to cache
    private var fruits: [Fruit] = [] //  This data is used to display on TableView

    // MARK: - View Cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        setupTableView()
        setupData()
    }

    private func setupData() {
        // Loading all fruits at the first time [Apple, Banana, Orange ]
        originalFruits = yourSetupDataFunctionToFetchAllFruitsAtTheFirstTime() 
        fruits = originalFruits
        tableView.reloadData()
    }

    @IBAction private func actionTapToSearchButton(_ sender: Any) {
        let searchingKey = searchTextField.text
        if searchingKey.isEmpty {
            fruits = originalFruits
        } else {
            fruits = yourSeacrchingFruitFunction(searchingKey)
        } 
        tableView.reloadData()
    }
}

// MARK: - UITableViewDataSource
extension ViewController: UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return fruits.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: FruitCell.identifier) as? FruitCell {
            return cell
        }
        return UITableViewCell()
    }
}

// MARK: - UITableViewDelegate
extension ViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    }
}

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

相关问题 如何在Swift中从另一个视图访问视图的表格视图的单元格? - How to access cells of a table view of a view from another view in Swift? 如何将多个选定的表视图单元格数据传输到Swift中的下一个视图控制器? - How do I transfer multiple selected table view cells data to the next view controller in Swift? 在SWIFT中滚动时,我的表视图重用所选单元格 - my table view reuse the selected cells when scroll — in SWIFT 表格视图帮助.....从一个表格视图中选择的单元格迅速传递到另一个表格视图 - Table view Help…..selected cells from one table view passed to another table view swift 表格视图未显示单元格快速 - Table View not showing the Cells Swift 如何从表格视图中的选定单元格中获取文本? - How to get the text from the selected cells in a table view? 如何首先快速将表格视图打开到选定的详细信息视图 - how to initially open a table view to a selected detail view in swift 在Swift中,当定向发生时,如何在表格视图中看到相同的单元格? - In Swift, how to see the same cells for a table view when an orientation happens? 如何在表格视图中删除空单元格? iOS Swift - How do I remove empty cells in table view? iOS Swift 如何在Swift 3中随机化表格视图单元格? - How do I randomize table view cells in Swift 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM