Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.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 以编程方式添加的按钮目标操作在自定义tableviewcell中不起作用_Ios_Swift_Button_Tableviewcell_Target Action - Fatal编程技术网

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

Ios 以编程方式添加的按钮目标操作在自定义tableviewcell中不起作用,ios,swift,button,tableviewcell,target-action,Ios,Swift,Button,Tableviewcell,Target Action,我以编程方式在自定义tableviewcell中添加了两个按钮,但目标操作不起作用。我能做些什么来解决这个问题?(swift) 这是我添加按钮的代码: override func awakeFromNib() { super.awakeFromNib() func createCueButton() -> UIButton{ let button = UIButton() button.titleLabel?.textColor = UIC

我以编程方式在自定义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)