问题描述
我想从内部NSObject数组数据中过滤NSObject数组。这是我的杰森回复
{"success":true,"message":"Invoice list","payload":{"invoice_list":[{"date":"2020-10-20","invoices":[{"id":41,"client_name":"Client 001"},{"id":43,"client_name":"Client 003"}]},{"date":"2020-10-19","invoices":[{"id":37,{"id":42,"client_name":"Client 002"}]},{"date":"2020-10-14","invoices":[{"id":36,"client_name":"Client 004"}]}]},"code":200}
这是我的阵列
var arrInvoiceList: [BossInvoiceDateObject] = []
这是我的NSObject类
class BossInvoiceDateObject: NSObject {
var date: String = ""
var arrInvoices: [BossInvoiceObject] = []
init(_ dictionary: [String: Any]) {
self.date = dictionary["date"] as? String ?? ""
if let invoices = dictionary["invoices"] as? [Dictionary<String,Any>] {
for i in 0..<invoices.count {
let objInvoice = BossInvoiceObject.init(invoices[i])
self.arrInvoices.append(objInvoice)
}
}
}
}
class BossInvoiceObject: NSObject {
var id: Int = 0
var client_name: String = ""
init(_ dictionary: [String: Any]) {
self.id = dictionary["id"] as? Int ?? 0
self.client_name = dictionary["client_name"] as? String ?? ""
}
}
这是我的UI屏幕截图链接:https://ibb.co/2j7ZDzb
现在我想从BossInvoiceObject类型的内部数组中搜索。
假设我在搜索中键入“客户端001” ,那么它应该像下面的响应一样过滤结果。
{"success":true,"client_name":"Client 001"}]},"client_name":"Client 001"}]}]},"code":200}
这是我的UITableView代码。
func numberOfSections(in tableView: UITableView) -> Int {
if self.searchActive {
return self.arrSearchedInvoiceList.count
} else {
return self.arrInvoiceList.count
}
}
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
if self.searchActive {
let objDateInvoice = self.arrSearchedInvoiceList[section]
let arrRows = objDateInvoice.arrInvoices
return arrRows.count
} else {
let objDateInvoice = self.arrInvoiceList[section]
let arrRows = objDateInvoice.arrInvoices
return arrRows.count
}
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BossInvoiceHistoryCell",for: indexPath) as! BossInvoiceHistoryCell
let objDateInvoice: BossInvoiceDateObject!
let objInvoice: BossInvoiceObject!
if self.searchActive {
objDateInvoice = self.arrSearchedInvoiceList[indexPath.section]
objInvoice = objDateInvoice.arrInvoices[indexPath.row]
} else {
objDateInvoice = self.arrInvoiceList[indexPath.section]
objInvoice = objDateInvoice.arrInvoices[indexPath.row]
}
cell.lblClientName.text = objInvoice.client_name
cell.lblAmount.text = objInvoice.currency + "\(objInvoice.invoice_total_amount)"
cell.selectionStyle = .none
return cell
}
我知道要像下面的代码那样从单个数组进行过滤。
self.arrSearchedInvoice = (self.arrActiveEmployee.filter{$0.client_name.localizedLowercase.localizedCaseInsensitiveContains(searchBar.text ?? "")} as NSArray) as! [BossInvoiceObject]
要求:我想根据内部对象的值进行搜索。
任何人都可以指导我实现这一目标。
谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)