ios - 以编程方式添加的按钮目标操作在自定义 tableviewcell 中不起作用

标签 ios swift button uitableview target-action

我在自定义 tableviewcell 中以编程方式添加了两个按钮,但目标操作不起作用。我该怎么做才能解决这个问题?(swift)

这是我添加按钮的代码:

override func awakeFromNib() {
    super.awakeFromNib()

    func createCueButton() -> UIButton{
        let button = UIButton()
        button.titleLabel?.textColor = UIColor.whiteColor()
        button.titleLabel?.font = UIFont.boldSystemFontOfSize(16.0)
        return button
    }

    completeButton = createCueButton()
    completeButton.setTitle("Complete", forState: UIControlState.Normal)
    completeButton.backgroundColor = UIColor.greenColor()
    completeButton.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)
    addSubview(completeButton)

    deleteButton = createCueButton()
    deleteButton.setTitle("Delete", forState: .Normal)
    deleteButton.backgroundColor = UIColor.redColor()
    deleteButton.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)
    addSubview(deleteButton)
}

func buttonPressed(sender: UIButton){

    var alertView = UIAlertView();
    alertView.addButtonWithTitle("OK");
    alertView.title = "Alert";
    alertView.message = "Button Pressed!!!";
    alertView.show();
}

override func layoutSubviews() {
    super.layoutSubviews()

    completeButton.frame = CGRect(x: 0, y: 0, width: cuesWidth, height: bounds.size.height)
    deleteButton.frame = CGRect(x: bounds.size.width - cuesWidth, y: 0, width: cuesWidth, height: bounds.size.height)
}

最佳答案

我更喜欢你问题的答案,请按照以下步骤操作

 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
 {
    return 75
 }

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
 {
    return 1
 }

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
 {
    let identifier = "Custom"

    var cell: CustomCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? CustomCell

    if cell == nil
    {
        tableViewCustom.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: identifier)

        cell = tableViewCustom.dequeueReusableCellWithIdentifier(identifier) as? CustomCell

    }

    //First Button for adding in Custom Cell
    let btn = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
    btn.backgroundColor = UIColor.greenColor()
    btn.setTitle("Complete", forState: UIControlState.Normal)
    btn.frame = CGRectMake(0, 6, 80, 40)
    btn.addTarget(self, action: "completeButtonTouched:", forControlEvents: UIControlEvents.TouchUpInside)
    cell.contentView.addSubview(btn)

    //Second Button for adding in CustomCell
    let btn2 = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
    btn2.backgroundColor = UIColor.greenColor()
    btn2.setTitle("Delete", forState: UIControlState.Normal)
    btn2.frame = CGRectMake(180, 5, 80, 40)
    btn2.addTarget(self, action: "deleteButtonTouched:", forControlEvents: UIControlEvents.TouchUpInside)
    cell.contentView.addSubview(btn2)


    return cell
}


func completeButtonTouched(sender:UIButton!)
{
    println("Complete Button Target Action Works!!!")
}


func deleteButtonTouched(sender:UIButton!)
{
    println("Delete Button Target Action Works!!!")
}

无论何时以编程方式将按钮添加到自定义单元格,请应用以下代码

 cell.contentView.addSubview(btn)

关于ios - 以编程方式添加的按钮目标操作在自定义 tableviewcell 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33052531/

相关文章:

android - 如何使用相应的 XML 文件创建 Activity ,只需点击一个按钮

javascript - 单击时,将短代码/bbcode 添加到文本区域?

ios - GLKit GLKVertexAttribColor : Dynamically changing colours

Swift:使具有相同 "shape"的两种类型符合通用协议(protocol)

ios - Swift 2 中的替换字符串

ios - 从 UITableView 选择行时将核心数据对象传递给另一个 View Controller

javascript - 以编程方式单击 Android WebView 中的按钮

objective-c - iOS:多个 View 访问相同的数据 - 如何访问?

iphone - 在 UIPageViewController 的底部放置一个栏

ios - 对标准附件 View 的约束