gpt4 book ai didi

ios - 在 uitableview 单元格中执行操作按钮以执行删除功能

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

我有一个表格 View ,在每个单元格中我都有用于删除特定单元格的 X 按钮。所以我已经完成了按钮的 iboutlet 到我的自定义表格 View 单元格。在索引路径的单元格行中,我已经将目标添加到我的按钮。但是当我在我的表格 View 单元格中按下我的按钮时。应用程序崩溃。如果我把断点。它显示在目标添加的代码行中。

这里是我的代码:

func followButton(sender: AnyObject) {


if let button = sender as? UIButton {
print("Button Clicked: \(button.tag)")
let item = Addtocartdata[button.tag]
print("name: \(item.cartid!)")

let headers = [
"cache-control": "no-cache",
"postman-token": "4c933910-0da0-b199-257b-28fb0b5a89ec"
]

let jsonObj:Dictionary<String, Any> = [
"cartID" : "\(item.cartid!)",
"cartType" : "3"
]


if (!JSONSerialization.isValidJSONObject(jsonObj)) {
print("is not a valid json object")
return
}

if let postData = try? JSONSerialization.data(withJSONObject: jsonObj, options: JSONSerialization.WritingOptions.prettyPrinted) {
let request = NSMutableURLRequest(url: NSURL(string: "http://exp.Cart.php")! as URL,
cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
///print(error)
} else {

DispatchQueue.main.async(execute: {

if let json = (try? JSONSerialization.jsonObject(with: data!, options: [])) as? Dictionary<String,AnyObject>
{
let status = json["status"] as? Int;

if(status == 1)
{

// self.tableView.reloadData()
print("items deleted")
self.tableView.reloadData()


}

}
})
}
})

dataTask.resume()
}


}

}

最佳答案

在Swift 3中,action的参数是一个选择器,语法是#selector(method-signature)。所以最终的代码是:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cartcel", for: indexPath) as! cartTableViewCell
cell.deleteButton.tag = indexPath.row;
cell.deleteButton.addTarget(self, action: #selector(followButton(sender:)), for: .touchUpInside)

return cell;
}

对于您的附加问题:

I need to get the value of each cell and i need to pass as a parameter to one api call. So if it does not mach. Then how can i do the selector. To perform the delete functionality

func followButton(sender: AnyObject) {
if let button = sender as? UIButton {
print("Button Clicked: \(button.tag)")
let item = Addtocartdata[button.tag]
print("name: \(item.cartproName), quality: \(item.proqty), price: \(item.cartproPrice)")
}
}

关于ios - 在 uitableview 单元格中执行操作按钮以执行删除功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40521200/

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