ios - 带有 URL 的 UITableViewCell

标签 ios swift uitableview

我正在创建一个涉及 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/

相关文章:

iOS 10 下载加密的 HLS 流

ios - 当 isMyLocationEnabled 设置为 true 时,GMSMapView 从哪里获取其位置数据?

ios - UIview 中的两个 TableView fatal error

ios - Xamarin TableView 崩溃

iOS 应用程序将保存密码但不会自动填充

objective-c - NSMutableArray 计算对象的出现次数,然后重新排列数组

iOS swift Wordpress api 获取不到json

cocoa - Swift 中 Printable 和 DebugPrintable 的区别

iphone - 如何在编辑模式下重新格式化自定义 UITableViewCell 以适应删除控件?

ios - memcpy() 在 iOS 中导致 EXC_BAD_ACCESS