我正在使用下面提到的数组填充tableView。如果用户搜索数组并找到项目,则应选择该单元格。
(
{
barcode = 8901058847857;
image = (
);
name = "Maggi Hot Heads";
productTotal = "60.00";
quantity = 3;
},
{
barcode = 8901491101837;
image = (
);
name = "Lays Classic Salted";
productTotal = "20.00";
quantity = 1;
}
)
我正在使用下面提到的代码来搜索数组中的条形码,但我不知道如何进一步进行,在其他情况下如何选择tableViewCell。
tableViewCell setSelected中编写了代码。
let namePredicate = NSPredicate(format: "barcode contains[c] %@",code)
let filteredArray = orderDetailsArray.filter { namePredicate.evaluate(with: $0) }
if filteredArray.isEmpty {
let alert = UIAlertController(title: "Alert", message: "Item not found in cart.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { action in
self.scan()
}))
self.present(alert, animated: true, completion: nil)
}else{
print("item found")
// Need to do selection here
}
最佳答案
else{
print("item found")
1. add to that object in separate array
yourMutableArr.append("scannedString") // which are scanned
recently
2. reload tableview
[yourTblView reloadData];
3. In cellForRowAtIndexPath compare that barcodes with datasource array's barcode
let strBarcode: String = dict["barcode"][indexPath.row]
if yourMutableArr.contains(strBarcode) {
//addNewItem
cell.accessoryType = .Checkmark
}
}
关于ios - 如何使用函数选择UITableViewCell而不点击它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43488878/