Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios uitableview单元格中的执行操作按钮以执行删除功能_Ios_Swift_Uitableview - Fatal编程技术网

Ios uitableview单元格中的执行操作按钮以执行删除功能

Ios uitableview单元格中的执行操作按钮以执行删除功能,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个表视图,在每个单元格中我有一个用于删除特定单元格的X按钮。因此,我对自定义表视图单元格执行了按钮的iboutlet。在索引路径的单元格行中,我完成了向按钮添加目标。但当我在表格视图单元格中按下按钮时。应用程序崩溃。如果我把刹车点放好。它显示在目标添加的代码行中 这是我的代码: func followButton(sender: AnyObject) { if let button = sender as? UIButton { print("B

我有一个表视图,在每个单元格中我有一个用于删除特定单元格的
X
按钮。因此,我对自定义表视图单元格执行了按钮的iboutlet。在索引路径的单元格行中,我完成了向按钮添加目标。但当我在表格视图单元格中按下按钮时。应用程序崩溃。如果我把刹车点放好。它显示在目标添加的代码行中

这是我的代码:

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()
            }


        }

    }
func followButton(发送方:AnyObject){
如果let button=发送方为UIButton{
打印(“单击按钮:\(Button.tag)”)
let item=Addtocartdata[button.tag]
打印(“名称:\(item.cartid!))
让标题=[
“缓存控制”:“无缓存”,
“邮递员代币”:“4c933910-0da0-b199-257b-28fb0b5a89ec”
]
让jsonObj:Dictionary=[
“cartID”:“\(item.cartID!)”,
“cartType”:“3”
]
if(!JSONSerialization.isValidJSONObject(jsonObj)){
打印(“不是有效的json对象”)
返回
}
如果让postData=try?JSONSerialization.data(带jsonobject:jsonObj,选项:JSONSerialization.WritingOptions.prettypted){
let request=NSMutableURLRequest(url:NSURL(字符串:)http://exp.Cart.php“”!作为URL,
cachePolicy:.useProtocolCachePolicy,timeoutInterval:10.0)
request.httpMethod=“POST”
request.allHTTPHeaderFields=标题
request.httpBody=postData
让session=URLSession.shared
让dataTask=session.dataTask(其中:request作为URLRequest,completionHandler:{(数据,响应,错误)->Void in
如果(错误!=nil){
///打印(错误)
}否则{
DispatchQueue.main.async(执行:{
如果让json=(尝试使用?JSONSerialization.jsonObject(使用:data!,选项:[])作为?字典
{
让status=json[“status”]as?Int;
如果(状态==1)
{
//self.tableView.reloadData()
打印(“删除的项目”)
self.tableView.reloadData()
}
}
})
}
})
dataTask.resume()
}
}
}

要从swift2.2或更高版本中添加选择器,语法已更改,您必须像这样添加目标

cell.deleteButton.addTarget(self, action:#selector(followButton(sender:)), for: .touchUpInside)
这就是如何获得该单元格的indexpath,使用indexpath可以完成其余的工作

func followButton(sender: UIButton!) {
    let point : CGPoint = sender.convert(CGPoint.zero, to:collectionView)
    let indexPath = collectionView.indexPathForItem(at: point)
    // here is your index path
}

要从swift2.2或更高版本中添加选择器,语法已更改,您必须像这样添加目标

cell.deleteButton.addTarget(self, action:#selector(followButton(sender:)), for: .touchUpInside)
这就是如何获得该单元格的indexpath,使用indexpath可以完成其余的工作

func followButton(sender: UIButton!) {
    let point : CGPoint = sender.convert(CGPoint.zero, to:collectionView)
    let indexPath = collectionView.indexPathForItem(at: point)
    // here is your index path
}

在Swift 3中,
action
的参数是选择器,语法是
#selector(方法签名)
。最后的代码是:

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;
}
关于你的补充问题:

我需要得到每个单元格的值,我需要作为参数传递 到一个api调用。所以如果它没有马赫数。那我怎么做呢 选择器。执行删除功能


在Swift 3中,
action
的参数是选择器,语法是
#selector(方法签名)
。最后的代码是:

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;
}
关于你的补充问题:

我需要得到每个单元格的值,我需要作为参数传递 到一个api调用。所以如果它没有马赫数。那我怎么做呢 选择器。执行删除功能


好的,我去看看。但是你能告诉我,我怎样才能得到每个单元格值的详细信息。就像在我的牢房里,我有名字,数量。因此,我需要将这些参数传递给我的api调用以执行删除功能。所以当我按下任何一个手机按钮。如何获取特定单元格的详细信息或值?此选择器不正确。它不考虑
followButton(发送者:)
函数上的参数。@rmaddy我会找到你的。我需要获取每个单元格的值,并将其作为参数传递给一个api调用。所以如果它没有马赫数。那我怎么做选择器呢。要执行删除功能,OK..将签出它。但是你能告诉我,我怎样才能得到每个单元格值的详细信息。就像在我的牢房里,我有名字,数量。因此,我需要将这些参数传递给我的api调用以执行删除功能。所以当我按下任何一个手机按钮。如何获取特定单元格的详细信息或值?此选择器不正确。它不考虑
followButton(发送者:)
函数上的参数。@rmaddy我会找到你的。我需要获取每个单元格的值,并将其作为参数传递给一个api调用。所以如果它没有马赫数。那我怎么做选择器呢。要执行删除功能,请不要将代码作为答案发布。一个好的答案解释了问题所在,也解释了答案是如何解决问题的。@Danh Huyng我正在获取索引路径。这很好。但是我怎样才能得到部分细胞的值呢?比如说,我有每个细胞的名称,价格,数量。当我按下任何单元格
X
按钮时。我怎样才能得到那个特定单元格的值你有数组包含这些东西吗?您填写表格的数据视图是的,请参阅我更新的帖子。我有一个单独的类叫做
Addtocartdata
,请不要只是把代码作为答案。一个好的答案解释了问题所在,也解释了答案是如何解决问题的。@Danh Huyng我正在获取索引路径。这很好。但是我怎样才能得到部分细胞的值呢?比如说,我有每个细胞的名称,价格,数量。当我按下任何单元格
X
按钮时。怎么