rarora25
rarora25

Reputation: 99

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.

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

Upvotes: 0

Views: 432

Answers (1)

Tam-Thanh Le
Tam-Thanh Le

Reputation: 333

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

IndexPaths would refresh correctly after TableView reloaded with a new collection of fruits

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

    }
}

Upvotes: 3

Related Questions