Данные ответа в формате Json
[
{
"STOCK": "20 Microns Ltd. EOD Prices",
"CODE": "BOM533022"
},
{
"STOCK": "3i Infotech Ltd. EOD Prices",
"CODE": "BOM532628"
},
{
"STOCK": "3m India Ltd. EOD Prices",
"CODE": "BOM523395"
},
{
"STOCK": "7seas Technologies Ltd-$ EOD Prices",
"CODE": "BOM590116"
},
]
Класс модели SearchPortfolioModel
import Foundation
typealias searchPortfolioModel = [SearchPortfolioModel]
struct SearchPortfolioModel: Codable {
let stock, code: String
enum CodingKeys: String, CodingKey {
case stock = "STOCK"
case code = "CODE"
}
}
PortfolioViewModel
func stockCodeValue(completion: @escaping () -> ()) {
portfolioClient.fetchSearchPortfolio(){ searchPortfolioModel in
self.searchPortfolioModel = searchPortfolioModel
completion()
}
}
struct PortfolioSearch{
var stockName: String
var stockCode: String
}
func portfolioSearchForItemAtIndexPath(indexPath: NSIndexPath) -> (SearchPortfolioModel){
let stockName = searchPortfolioModel?[indexPath.row].stock ?? ""
let stockCode = searchPortfolioModel?[indexPath.row].code ?? ""
return SearchPortfolioModel(stock: stockName, code:stockCode)
}
Внутри класса ViewController добавлен UITableView & UISearchBar.Как искать данные из UISearchBar на основе предоставленного ответа.В ViewController все данные разбора отображаются в виде таблицы
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
if(searchActive) {
return filtered.count
}
// return data.count;
return portfolioViewModel.numberOfItemsInPortfolioSearchSection(section: section)
}
func configurationNewsCell (cell: AddPortfolioTableViewCell, forRowAtIndexPath indexPath:NSIndexPath){
let searchPortfolioStocks = portfolioViewModel.portfolioSearchForItemAtIndexPath(indexPath: indexPath as NSIndexPath)
// Filtered data for stock & code need to will update based on search string.
if(searchActive){
cell.lbl_PortfolioStockName?.text = filtered[indexPath.row]
cell.lbl_PortfolioStockCodeName?.text = searchPortfolioStocks.code
} else {
cell.lbl_PortfolioStockName?.text = searchPortfolioStocks.stock
cell.lbl_PortfolioStockCodeName?.text = searchPortfolioStocks.code
}
}
Метод делегирования UISearchBar.
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filtered = searchPortfolioStocks.filter{ $0.stock.range(of: searchText, options: .caseInsensitive) != nil }
if(filtered.count == 0){
searchActive = false;
} else {
searchActive = true;
}
self.tbl_PortfolioSearch.reloadData()
}
Я хочу выполнить поиск с использованием фондового и кодового имени!