【问题标题】:What is wrong with my UISearchBar result?我的 UISearchBar 结果有什么问题?
【发布时间】:2023-03-05 15:19:01
【问题描述】:

我已经使用 Alamofire 和 SwiftyJSON 解析了一个 JSON,以获取加密货币名称的列表并填充表格视图。然后我尝试使用 UISearchBar 过滤结果,但无论我搜索什么,它总是显示名称数组的第一个结果,即使过滤器正在工作,因为我使用 print 测试它并且它正确过滤。例如,我搜索 Ethereum 并且控制台打印 ["name": "Ethereum"] 但 Tableview 将始终显示“Bitcoin”,如果我搜索一个不存在的名称,它不会显示任何内容。谁能确定我的代码中有什么问题导致了这个问题?非常感谢,我对任何类型的编码都不熟悉。

这是我的代码:

//
//  StartViewController.swift
//  CryptoClockTesting
//
//  Created by Peter Ruppert on 08/07/2018.
//  Copyright © 2018 Peter Ruppert. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON

class StartViewController: UIViewController, UISearchBarDelegate, 
UITableViewDelegate, UITableViewDataSource {

var names = [[String:String]]()

var isSearching = false

var filteredData = [[String:String]]()




@IBOutlet weak var tableView: UITableView!

@IBOutlet weak var searchBar: UISearchBar!

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    let name = names[indexPath.row]
    let text: [String:String]

    if isSearching {
        text = filteredData[indexPath.row]
        print(text)


    } else {
        text = name
    }

    cell.textLabel?.text = name["name"]
    cell.textLabel?.textColor = UIColor.blue


    return cell
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {



    if searchBar.text == nil || searchBar.text == "" {



        isSearching = false

        view.endEditing(true)

        tableView.reloadData()
    } else {
        isSearching = true


        filteredData = names.filter({
            $0["name"] == searchBar.text
        })

        tableView.reloadData()
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if isSearching {
        return filteredData.count
    } else {
    return names.count
}
}

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    tableView.isHidden = false

}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    tableView.isHidden = true
}




override func viewDidLoad() {
    super.viewDidLoad()




    tableView.isHidden = true

    searchBar.delegate = self
    tableView.delegate = self
    tableView.dataSource = self
    searchBar.returnKeyType = UIReturnKeyType.done

    self.view.backgroundColor = .blue



    let urlString = "https://api.coingecko.com/api/v3/coins"

    if let url = URL(string: urlString) {
        if let data = try? String(contentsOf: url) {
            let json = JSON(parseJSON: data)


            parse(json: json)


        }

    }
}



//Parsing News API and adding to table, then use table view setup to properly display.
func parse(json: JSON) {
    for result in json[].arrayValue {
        let name = result["name"].stringValue
        let obj = ["name": name]

        names.append(obj)

    }
    tableView.reloadData()
}

}

【问题讨论】:

  • 请仔细阅读我对你previous question的回复????
  • @vadian 啊,是的,你是对的,我看错了。抱歉!

标签: swift xcode uitableview uisearchbar


【解决方案1】:

这是因为您总是在cellForRowAt 中将其设置为name["name"]

if isSearching {
    text = filteredData[indexPath.row]
    print(text)


} else {
    text = name
}

cell.textLabel?.text = name["name"] // should be text["name"]

【讨论】:

  • 完美!谢谢!花了很多时间改变周围的东西,但一定错过了,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
  • 2018-04-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多