【发布时间】:2016-12-10 21:17:13
【问题描述】:
我想使用 NSMutableArray 在动态表视图中添加搜索,实际上我正在使用 json 在表中添加产品,所以我需要 NSMutableArray
首先我按原样分配值
var filteredItems = [String]()
var searchController:UISearchController!
var products:NSMutableArray = NSMutableArray()
我正在使用 moltin API 并按原样使用 NSArray..
if let newProducts:NSArray = response?["result"] as? NSArray {
self.products.addObjects(from: newProducts as [AnyObject])
}
let responseDictionary = NSDictionary(dictionary: response!)
if let newOffset:NSNumber = responseDictionary.value(forKeyPath: "pagination.offsets.next") as? NSNumber {
self.paginationOffset = newOffset.intValue
我也用这个做单元格
let cell = tableView.dequeueReusableCell(withIdentifier: CELL_REUSE_IDENTIFIER, for: indexPath) as! ProductsListTableViewCell
let row = (indexPath as NSIndexPath).row
let product:NSDictionary = products.object(at: row) as! NSDictionary
cell.configureWithProduct(product)
return cell
鉴于确实加载了导航栏上的搜索栏,我使用了正确的方法。
我正在使用这个函数来过滤结果,但是它不起作用并报错:
无法将值转换为类型 (_) -> _ 转换为接受的参数类型 'NSPredict'
func updateSearchResults(for searchController: UISearchController) {
filteredItems.removeAll(keepingCapacity: false)
// filter
filteredItems = products.filter{
item in
item.lowercased().contains(searchController.searchBar.text!.lowercased())
}
tableView.reloadData()
}
但是当我使用简单的项目数组时,它对我来说效果很好。我如何在 NSMutableArray 中使用它?
【问题讨论】:
-
您的
products包含什么?只有String(NSString)? -
实际上我使用 moltin API 作为 DB 并使用 NSArray
-
我不知道 moltin API,因此无法了解 你们的产品包含什么。
-
` if let newProducts:NSArray = response?["result"] as? NSArray { self.products.addObjects(from: newProducts as [AnyObject]) }
-
如何将我的代码图片发送给您,以便您了解我的问题??
标签: ios swift uitableview nsmutablearray