gpt4 book ai didi

ios - 在 iOS 中从 NSArray 的 UITableView 删除一行并更新 UI

转载 作者:行者123 更新时间:2023-11-30 11:47:09 25 4
gpt4 key购买 nike

我需要删除一个表格 View 单元格。删除操作将从服务器中删除数据并成功弹出。但 TableView 中的单元格不会被删除。我们如何从 NSArray 中删除数据? [.remove(at: indexpath.row)] 不适用于 NSArray。谁能帮我解决这个问题?

import UIKit
import Alamofire

class BankDashController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var dictData:NSArray = NSArray()
var dictData2 = [String]()
var appDictionary:NSDictionary!
var strId: NSArray?
var appDictionary1: NSNumber?
var TokenSet = UserDefaults.standard.object(forKey: "tokenSetAll")

@IBOutlet weak var bankTable: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

self.getTaskData()

bankTable.delegate = self
bankTable.dataSource = self
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.getTaskData()
}

@IBAction func addBankDetails(_ sender: Any) {
let navigati:BankDetailsController = storyboard!.instantiateViewController(withIdentifier: "BankDetailsController") as! BankDetailsController
self.navigationController?.pushViewController(navigati, animated: true)
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dictData.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:BankDashCell = tableView.dequeueReusableCell(withIdentifier: "cellDetail" as String) as! BankDashCell!

self.appDictionary = self.dictData.object(at: indexPath.row) as! NSDictionary
cell.bankName?.text = self.appDictionary.value(forKey: "bank_name") as? String
cell.bankDate?.text = self.appDictionary.value(forKey: "last_visit_date") as? String
cell.accountHolderName?.text = self.appDictionary.value(forKey: "account_holder_name") as? String
cell.accountNumber?.text = self.appDictionary.value(forKey: "account_number") as? String
cell.contactNumber?.text = self.appDictionary.value(forKey: "contact") as? String
cell.bankView?.layer.cornerRadius = 5
return cell
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
print("self.dictData2",self.dictData)

self.strId = (self.dictData.value(forKey: "id") as AnyObject) as? NSArray
print("ids",self.strId! as Any)

self.appDictionary1 = self.strId!.object(at: indexPath.row) as? NSNumber

print("ids delete",self.appDictionary1!)

let strinDuration:NSString = NSString(format:"http://54.89.111.171/api/user-bank-delete/%@?token=%@",self.appDictionary1!,self.TokenSet as! CVarArg) as String as String as NSString

print("strinDuration2",strinDuration)
let collectionviewone: String = strinDuration as String

Alamofire.request(collectionviewone, method: .get, parameters: nil, encoding: JSONEncoding.default)
.responseJSON { response in
debugPrint(response.result)
if let JSON = response.result.value{

let statu = (JSON as AnyObject).value(forKey: "status") as! NSNumber

if statu .isEqual(to: 1)

{
let alertController = UIAlertController(title: "Completed Sucessfully!!!", message: "Thank You.", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(defaultAction)
self.present(alertController, animated: true, completion: nil)
}

self.bankTable?.reloadData()
}
}
}
}

func getTaskData(){
self.bankTable.isHidden = true
let strinDuration:NSString = NSString(format:"http://54.89.111.171/api/user-bank-detail?token=%@",TokenSet as! CVarArg) as NSString
let collectionviewone: String = strinDuration as String

Alamofire.request(collectionviewone, method: .get, parameters: nil, encoding: JSONEncoding.default)
.responseJSON { response in
debugPrint(response.result)
if let JSON = response.result.value{

self.dictData = (JSON as AnyObject)["data"] as! NSArray
print("self.dictData",self.dictData)

if self.dictData.count > 0{

self.bankTable.isHidden = false
self.bankTable?.reloadData()

}else{
}
}
}
}
}

最佳答案

使 dictData 数组可变 var dictData:NSMutableArray = NSMutableArray()

然后尝试dictData.removeObject(at:indexPath.row)

关于ios - 在 iOS 中从 NSArray 的 UITableView 删除一行并更新 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48699002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com