我正在用OrderDetailsArray填充我的tableView。然后在DidSelect上,我给reorderArray增加了价值。如何在点击DidDeSelect时从数组中删除值
我的tableView委托的功能如下:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let object = orderDetailsArray.object(at: (indexPath as NSIndexPath).row) as! NSDictionary
let storeID = UserDefaults.standard.value(forKey: DefaultsKey.storeID.rawValue) as! Int
let barcode = object["barcode"] as! String
let mrp = object["mrp"] as! String
let productDiscount = object["product_discount"] as! String
reorderDictionary.setObject((storeID), forKey: "store_id" as NSCopying)
reorderDictionary.setObject((barcode), forKey: "barcode" as NSCopying)
reorderDictionary.setObject((mrp), forKey: "mrp" as NSCopying)
reorderDictionary.setObject((productDiscount), forKey: "product_discount" as NSCopying)
reorderArray.add(reorderDictionary)
let cell = tableView.cellForRow(at: indexPath) as! OrderHistoryProductDetailsTableViewCell
cell.contentView.backgroundColor = UIColor.red
print(reorderArray)
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! OrderHistoryProductDetailsTableViewCell
let object = reorderArray.object(at: (indexPath as NSIndexPath).row) as! NSDictionary
reorderArray.remove(object)
cell.contentView.backgroundColor = UIColor.clear
print(reorderArray)
}reorderArray为NSMutableArray()
发布于 2017-05-18 19:11:38
除非有充分的理由,否则您不应该在Swift中使用诸如NSMutableArray或NSDictionary之类的基础类,但简单的答案是您可以使用remove从NSMutableArray中删除对象的实例,并且您可以很容易地从索引路径中找到相关对象:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! OrderHistoryProductDetailsTableViewCell
let object = orderDetailsArray.object(at: (indexPath as NSIndexPath).row) as! NSDictionary
reorderArray.add(object)
cell.contentView.backgroundColor = UIColor.red
print(reorderArray)
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let object = orderDetailsArray.object(at: (indexPath as NSIndexPath).row) as! NSDictionary
reorderArray.remove(object)
let cell = tableView.cellForRow(at: indexPath) as! OrderHistoryProductDetailsTableViewCell
cell.contentView.backgroundColor = UIColor.clear
print(reorderArray)
}发布于 2017-05-18 20:09:46
在Swift 3.0中,你可以尝试这样做,当使用Array of Dictionary时,你可以这样做。
func tableView(_ tableView: UITableView, didDeSelectRowAt indexPath: IndexPath)
{
let dicDetails = arrUserDetails[indexPath.row] as! NSMutableDictionary
dicDetails.removeObject(forKey: "your_Key")
}发布于 2017-05-18 22:26:24
重试一次
1.Create One more array globally and store indexpaths insted of reorderArray
OR
2.Apply this lines of code in TableViewCellForRowAtIndexPath:
if reorderArray.contains(reorderDictionary){
cell.contentView.backgroundColor = UIColor.red
}else{
cell.contentView.backgroundColor = UIColor.clear
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let object = orderDetailsArray.object(at: (indexPath as NSIndexPath).row) as! NSDictionary
let storeID = UserDefaults.standard.value(forKey: DefaultsKey.storeID.rawValue) as! Int
let barcode = object["barcode"] as! String
let mrp = object["mrp"] as! String
let productDiscount = object["product_discount"] as! String
reorderDictionary.setObject((storeID), forKey: "store_id" as NSCopying)
reorderDictionary.setObject((barcode), forKey: "barcode" as NSCopying)
reorderDictionary.setObject((mrp), forKey: "mrp" as NSCopying)
reorderDictionary.setObject((productDiscount), forKey: "product_discount" as NSCopying)
let cell = tableView.cellForRow(at: indexPath) as! OrderHistoryProductDetailsTableViewCell
if reorderArray.contains(reorderDictionary){
reorderArray.remove(reorderDictionary)
cell.contentView.backgroundColor = UIColor.clear
}else{
reorderArray.add(reorderDictionary)
cell.contentView.backgroundColor = UIColor.red
}
print(reorderArray)
}https://stackoverflow.com/questions/44045550
复制相似问题