Search code examples
iosswiftbuttonuitableviewtarget-action

Programmatically added buttons target action not working in custom tableviewcell


I added programmatically two buttons in custom tableviewcell, but target action not working. What can i do to solve this problem ?(swift)

This is my code of adding buttons:

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

Solution

  • I preferred the answer for your question, please follow the below steps

     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!!!")
    }
    

    Whatever when add the button to custom cell programmatically, please apply the below code

     cell.contentView.addSubview(btn)