不应从SearchBar

时间:2018-11-17 12:22:46

标签: swift uitableview uisearchbar

当我在搜索栏中输入单词时,对数组进行过滤是不好的。

键入内容时,不会过滤结果。

当我输入字母D(例如D)时,我得到的结果列表与搜索不匹配

这是代码。你能帮忙吗?谢谢。

代码:

import UIKit
import Alamofire
import SwiftyJSON


class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate {

   @IBOutlet weak var tableView: UITableView!
   @IBOutlet weak var searchBar: UISearchBar!

   let URL_DATA = "https://api.coincap.io/v2/assets"

   var coin = [CoinModel]()
   var searchCoin = [String]()
   var searching = false

   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

       if searching {
           return searchCoin.count
       } else {
           return coin.count
       }
   }

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CoinTableViewCell

       tableView.backgroundColor = .clear
       cell.backgroundColor = .clear

       if searching{
           cell.nameLabel.text = searchCoin[indexPath.row]
       } else {
           cell.nameLabel.text = coin[indexPath.row].name
       }
       let coindata: CoinModel
       coindata = coin[indexPath.row]

       cell.nameLabel.text = coindata.name
       cell.symbolLabel.text = coindata.symbol
       let priceFloat = (coindata.price as NSString).floatValue
       let costString = String(format:"%.3f", priceFloat)
       cell.priceLabel.text = "\(costString)"
       let percentFloat = (coindata.percent as NSString).floatValue
       let percentString = String(format:"%.2f", percentFloat)

       if percentFloat < 0 {
           cell.percentLabel.text = "\(percentString)%"
           cell.percentLabel.textColor = UIColor(hexString: "#F47E99")
       } else {
            cell.percentLabel.text = "+\(percentString)%"
            cell.percentLabel.textColor = UIColor(hexString: "#19984A")
       }
       return cell
   }

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

       searchCoin = coin.filter( {$0.name.prefix(searchText.count) == searchText} ).map( {$0.name} )
       searching = true
       tableView.reloadData()
   }

   func getCoinData() {
    Alamofire.request(URL_DATA).responseJSON { response in

        if let json = response.result.value as? [String: Any], let arr = json["data"] as? [[String: Any]] {
            let CoinArray: [[String: Any]] = arr

            for dict in CoinArray {
                if let name = dict["name"] as? String,
                    let symbol = dict["symbol"] as? String,
                    let price = dict["priceUsd"] as? String,
                    let percent = dict["changePercent24Hr"] as? String   
                   {
                       let coinnModal: CoinModel = CoinModel(name: name, symbol: symbol, price: price, percent: percent)
                       self.coin.append(coinnModal)
                   }
                   self.tableView.reloadData()
               }
           }
       }
   }

   override func viewDidLoad() {
       super.viewDidLoad()
       getCoinData()
   }
}

这是模拟器的屏幕截图

搜索结果不好

https://imgur.com/a/IHVAM13

这也是一个模型:

型号:

import Foundation

class CoinModel {

   var name: String
   var symbol: String
   var price: String
   var percent: String

   init(name: String, symbol: String, price: String, percent: String) {
      self.name = name
      self.symbol = symbol
      self.price = price
      self.percent = percent
   }
}

0 个答案:

没有答案