[英]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.