我正在创建一个涉及自定义tableviewcell的taleview的应用程序。我在tableviewcontroller中使用三个数组来在tableview中显示。一旦用户选择该行,它就会根据我创建的urlArray将它们带到url。这完全正常,直到我在tableview中实现搜索,然后当我搜索tableview并单击一个单元格转到我的数组内的第三个或第四个url时,这将是错误的url。
这是tableviewcontroller的代码。
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var tableview: UITableView!
var arrUrlName = ["https://www.zacks.com/stock/quote/AAPL?q=aapl?q=AAPL?q=AAPL","https://www.zacks.com/stock/quote/GOOG?q=goog?q=GOOG?q=GOOG","https://www.zacks.com/stock/quote/FB?q=fb?q=FB?q=FB","https://www.zacks.com/stock/quote/AMZN?q=amzn?q=AMZN?q=AMZN"]
var stockNames = ["Apple Inc.","Google Inc.","Facebook Inc.","Amazon"]
var symbolArray = ["AAPL","GOOG","FB","AMZN"]
let cellIdentifier = "Cell"
var filteredData: [String]!
override func viewDidLoad() {
super.viewDidLoad()
filteredData = stockNames
self.tableview.dataSource = self
self.tableview.delegate = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.filteredData.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! StockTableViewCell
let arrayName = filteredData[indexPath.row]
let arraySymbol = symbolArray[indexPath.row]
cell.stockNameLabel?.text = arrayName
cell.symbolLabel?.text = arraySymbol
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let urlName = self.arrUrlName[indexPath.row]
let url = URL(string: urlName)
if #available(iOS 10.0, *) {
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url!)
}
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
// When there is no text, filteredData is the same as the original data
if searchText.isEmpty {
filteredData = stockNames
} else {
// The user has entered text into the search box
// Use the filter method to iterate over all items in the data array
// For each item, return true if the item should be included and false if the
// item should NOT be included
filteredData = stockNames.filter({(dataItem: String) -> Bool in
// If dataItem matches the searchText, return true to include it
if dataItem.range(of: searchText, options: .caseInsensitive) != nil {
return true
} else {
return false
}
})
}
tableview.reloadData()
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
searchBar.showsCancelButton = true
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.showsCancelButton = false
searchBar.text = ""
searchBar.resignFirstResponder()
}
}
答案 0 :(得分:1)
您需要更好地整理数据。而不是三个单独的数组,有一个数组。数组的每个元素应该是具有三个值或更好的字典,一个struct
具有三个属性。
这使得对表格视图的数据进行排序和过滤变得更加容易。
答案 1 :(得分:0)
问题是您正在过滤stockNames
,但保留arrUrlName
和symbolArray
未过滤。选择行后,您在这里选择了错误的网址:
let urlName = self.arrUrlName[indexPath.row]
因为stockNames
和arrUrlName
的索引不再同步。
推荐 解决方案:
解决此问题的最简单方法是在单个阵列中重新组织数据。 首先,创建结构
struct TableItem {
let url: String
let name: String
let symbol: String
}
然后,重新组织单个数组中的数据,如
let data = [
TableItem(
url: "https://www.zacks.com/stock/quote/AAPL?q=aapl?q=AAPL?q=AAPL",
name: "Apple Inc.",
symbol: "AAPL"
),
TableItem(
url: "https://www.zacks.com/stock/quote/GOOG?q=goog?q=GOOG?q=GOOG",
name: "Google Inc.",
symbol: "GOOG"
),
TableItem(
url: "https://www.zacks.com/stock/quote/FB?q=fb?q=FB?q=FB",
name: "Facebook Inc.",
symbol: "FB"
),
TableItem(
url: "https://www.zacks.com/stock/quote/AMZN?q=amzn?q=AMZN?q=AMZN",
name: "Amazon",
symbol: "AMZN"
)
]
设置初始filteredData
var filteredData = data
使用以下内容填充cellForRowAt:
中的单元格数据
let item = filteredData[indexPath.row]
cell.stockNameLabel?.text = item.name
cell.symbolLabel?.text = item.symbol
修正过滤:
filteredData = data.filter { $0.name.range(of: searchText, options: .caseInsensitive) != nil }
所以方法成为:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.isEmpty {
filteredData = data
} else {
filteredData = data.filter { $0.name.range(of: searchText, options: .caseInsensitive) != nil }
}
tableview.reloadData()
}
这样,符号和网址也会被过滤掉。
在didSelectRowAt:
中,您可以获取您的网址:
let item = filteredData[indexPath.row]
let url = URL(string: item.url)