Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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 Tableview button.tag抛出lldb_Ios_Swift_Uitableview_Swift3_Tableview - Fatal编程技术网

Ios Tableview button.tag抛出lldb

Ios Tableview button.tag抛出lldb,ios,swift,uitableview,swift3,tableview,Ios,Swift,Uitableview,Swift3,Tableview,我不知道发生了什么,我用表行设置了按钮。tag,当它到达row>1时,它将抛出lldb。如果button.tag要获得包含点击按钮的单元格的indexath,可以使用类似于此的函数来满足您的需求 func showAlert(sender: AnyObject) { if let cell = sender.superview?.superview as? UITableViewCell{ // do check your viewchierarchy in your cas

我不知道发生了什么,我用表行设置了
按钮。tag
,当它到达row>1时,它将抛出lldb。如果
button.tag要获得包含点击按钮的单元格的indexath,可以使用类似于此的函数来满足您的需求

 func showAlert(sender: AnyObject) {
         if let cell = sender.superview?.superview as? UITableViewCell{ // do check your viewchierarchy in your case
              let indexPath = itemTable.indexPath(for: cell)
           }
         print(indexPath)// you can use this indexpath to get index of tapped button
     }
从cellforrowatinexpath
alertBtn.tag=indexPath.row中删除此行

若您可以使用自定义单元格来实现此目的,那个么您可以像以前一样获取所选按钮的indexpath


为按钮和标签等创建CustomCell和IBOutlet。您可以在cellForRowAtIndexPath中访问单元格的子视图,并为按钮指定标记。如果您对CustomCell有任何疑问,请告诉我

应用程序在这一行崩溃,因为它找不到带有标记1的视图,因此标记在每个单元格中以行值更新

let alertBtn = cell.viewWithTag(1) as! UIButton

删除此行,并从UITableViewCell获取警报BTN的@IBOutlet,而不是使用标签刷新。

Swift 3X…

您正在替换标记,因此第一个标记项为零,因此请替换此代码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cells")! as UITableViewCell
let alertBtn = cell.viewWithTag(1) as! UIButton
alertBtn.addTarget(self, action: #selcetor(showAlert(sender:))), for: .touchUpInside)
return cell
 }

func showAlert(sender:UIButton) {
        let point = sender.convert(CGPoint.zero, to: self.tableview)
        let indexpath =  self.tableview.indexPathForRow(at: point)
}

尝试执行自定义
UITableViewCell

为新类声明协议和委托。连接一个动作并调用代理

protocol MyCellDelegate: class {
    func buttonPressed(for cell: MyCell)
}

class MyCell:UITableViewCell {

    weak var delegate: MyCellDelegate?

    @IBAction func buttonPressed(sender: Any){
        self.delegate?.buttonPressed(for: self)
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    .......
    cell.delegate = self

    ........
}

记住在VC中添加新的协议实现。您可以添加
prepareforeuse
方法,并在重复使用单元格时将委托重置为零。

它会在何处引发错误。您能否向我们展示showAlert的实现。错误来自于我设置按钮标记时,即使我删除了添加目标,它也会继续引发相同的错误。它与添加目标无关。您在let alertBtn=cell.viewWithTag(1)as中尝试执行的操作!UIButton;线这里是说,带有标签1的视图是您的按钮,然后您再次想要更改标签,因此在重用状态下,您的标签会发生变化,并且可能导致崩溃的原因是这条线。为什么要从视图中获取带有标记的按钮。您在其中添加了“alertBtn”?您可以显示自定义的手机swift文件吗?谢谢,它可以工作!!!,但是现在的add目标应该是这样的。alertBtn.addTarget(self,action:#选择器(showAlert(sender:)),for:.touchUpInside)
protocol MyCellDelegate: class {
    func buttonPressed(for cell: MyCell)
}

class MyCell:UITableViewCell {

    weak var delegate: MyCellDelegate?

    @IBAction func buttonPressed(sender: Any){
        self.delegate?.buttonPressed(for: self)
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    .......
    cell.delegate = self

    ........
}