gpt4 book ai didi

ios - 带有 URL 的 UITableViewCell

转载 作者:行者123 更新时间:2023-11-28 15:53:33 24 4
gpt4 key购买 nike

我正在创建一个涉及 taleview 和自定义 tableviewcell 的应用程序。我在我的 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()
}
}

最佳答案

您需要更好地组织数据。用一个数组代替三个单独的数组。数组的每个元素应该是具有三个值的字典,或者更好的是,一个具有三个属性的 struct

这使得为表格 View 排序和过滤数据变得更加容易。

关于ios - 带有 URL 的 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42058549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com