handle uibutton click in uitableview cause error - ios

I've got a UITableViewCell that contains a button, I handle this button click event follow this link Get button click inside UI table view cell,
Here is my code:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
cell.directCommentButton.tag = indexPath.row;
cell.directCommentButton.addTarget(self, action: "directCommentButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
func directCommentButtonClicked(sender: AnyObject) {
// directComment = 1
println("DIRECT COMMENT")
}
but there is an error directCommentButtonClicked]: unrecognized selector sent to instance 0x7f9e50590f10' and app has crashed. When I remove the sender: AnyObject, the error disappear, but I want to get the sender, how can I do that. Tks in advance

Just pass your action selector this way:
cell.directCommentButton.addTarget(self, action: "directCommentButtonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
you have to add : because your function accepts an argument.

Related

UIButton within UITableViewCell issue: can't get recognized selector

So I've been searching StackExchange on how to put a UIButton inside of a UITableViewCell and thought I found the answer, but keep getting an "unrecognized selector sent to instance" error.
Here's where I call the function
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tblTeams.dequeueReusableCell(withIdentifier: "cellReuse")! as! TeamsTableViewCell
cell.btnLeave.tag = indexPath.row
cell.btnLeave.addTarget(self, action: "LeaveTeam:", for: UIControlEvents.touchUpInside)
return cell
}
and here's where the function is. It's within the same class, and even extension, as the prior code block
#IBAction func LeaveTeam(sender: UIButton){
}
I've tried rewording the quote, I've tried using #selector... just please tell me how I do it right. Thanks!
Replace
cell.btnLeave.addTarget(self, action: #selector(leaveTeam(_:)) for: UIControlEvents.touchUpInside)
#objc func leaveTeam(_ sender: UIButton) {---}
start method name in lower case
addTarget(self, action:#selector(leaveTeam(sender:)), for: .touchUpInside)
#objc func leaveTeam(sender : UIButton) -> Void {
}

Swift 3 - button in .xib custom tableviewcell is not performing action

I have a tableviewcontroller, where i have created custom tableviewcell in a .xib files.
The button (myButton) is used on myCustomTableViewCell.xib has an outlet to myCustomTableViewCell.swift
I have set an action for the button in the TableViewController.swift file
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("myCustomTableViewCell", owner: self, options: nil)?.first as! myCustomTableViewCell
cell.myButton.setTitle(arrayOfMyData[indexPath.row].myText, for:.normal )
//other code to set some other cell.attribute values like above (all works)
cell.myButton.actions(forTarget: "myAction", forControlEvent: .touchUpInside)
}
and elsewhere in the TableViewController.swift I have the action:
func myAction(sender: UIButton){
print("pressed myButton")
}
but myAction is never called / "pressed myButton" is never printed. What am I missing?
You need to use addTarget(_:action:for:) method to add the action for your button not the actions(forTarget:forControlEvent:).
The function actions(forTarget:forControlEvent:) returns the actions that have been added to a control. It doesn't add a target/action.
cell.myButton.addTarget(self,action: #selector(myAction(sender:)), for: .touchUpInside)

Implementing accessoryButtonTappedForRowWithIndexPath: in Swift 2

I'm attempting to implement accessoryButtonTappedForRowWithIndexPath: in Swift 2 on a UITableViewController.
As I explain below, I think I'm missing something in when I create the disclosureIndicator, but I don't know what. It gets drawn from code, but my target action doesn't get called. UGH!
To do this programmatically, my understanding is I need to add the detailDisclosure indicator in cellForRowAtIndexPath before my cell is returned. I'm doing that as follows:
// Create disclosure indicator button in the cell
let disclosureIndicatorButton = UIButton(type: UIButtonType.DetailDisclosure)
disclosureIndicatorButton.addTarget(self, action: "disclosureIndicatorPressed:event:", forControlEvents: UIControlEvents.TouchUpInside)
customCell.accessoryType = .DisclosureIndicator
In my code, the detailDisclosure chevron gets drawn, but the target action method I assigned to it doesn't get called.
Then I need to create a handler for the button when it's pressed:
func disclosureIndicatorPressed(sender: UIButton, event: UIControlEvents) {
print("disclosure button pressed")
// convert touches to CGPoint, then determine indexPath
// if indexPath != nil, then call accessoryButtonTappedForRowWithIndexPath
}
Finally accessoryButtonTappedForRowWithIndexPath contains code to perform the segue, which I can do. What am I missing?
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/#//apple_ref/occ/intfm/UITableViewDelegate/tableView:accessoryButtonTappedForRowWithIndexPath:
Not sure why you are adding disclosure button indicator like that in code.
What you are looking for is simply 2 step process -
Step 1 : Add correct accessoryType on cell:
cell.accessoryType = .DetailDisclosureButton
Step 2 : Take action when the button is tapped by creating the accessoryButtonTappedForRowWithIndexPath function:
override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
doSomethingWithItem(indexPath.row)
}

How to get click event UIbutton on tableView cell?

I have taken UIbutton on TableViewCell, but when I click on a row, only the row get clicked, but I want to click only the button on that row.
How is this possible?
In the CellForRowatindexPath define tag for button and also set target to handler the event
button.tag=indexPath.row;
button.addTarget(self, action: "buttonHandler:", forControlEvents: UIControlEvents.TouchUpInside)
*************
func buttonHandler(sender:UIButton!)
{
if(sender.tag==0){
println("Button at row 0")
}
else if(sender.tag==1){
println("Button at row 1")
}
}
you set click event for cell Button try this way
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
cell.yourButton.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
}
func buttonClicked(sender:UIButton!)
{
println("Button tapped")
}
First do one thing
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// This will disable the selection highlighting.
Then create IBOutlet of your button in Cell class (Don't create the IBAction yet!!!)
Then in your CellForRowatindexPath create the action of button like this
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
then create the button action Function
func buttonAction(sender:UIButton!)
{
println("Button tapped")
}
Check it. Hope it helps!!!!

Swift - Custom TableView Not Executing Click on UIButton

I am trying to do what I believed was a simple task using Swift. I have created a Custom TableView Cell and added a button to it. I want to be able to just see when you click on the button some action take place. Unfortunately, when I click on the button there is nothing happening. Can someone shed some light on this? I have placed the code below:
Custom TableView Cell:
import UIKit
class TestTableViewCell: UITableViewCell {
#IBOutlet weak var testBtn: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func buttonTappedOnCell()
{
println("buttonTapped!")
}
}
Below is the code in the View Controller which the call is being made:
func tableView(tableView: UITableView,
cellForRowAtIndexPath
indexPath: NSIndexPath) -> UITableViewCell {
tableView.registerNib(UINib(nibName:"TestTableViewCell", bundle:nil), forCellReuseIdentifier:"TestButtonCell")
let cell =
tableView.dequeueReusableCellWithIdentifier("TestButtonCell", forIndexPath: indexPath) as TestTableViewCell
cell.testBtn.addTarget(cell, action: "buttonTappedOnCell", forControlEvents: UIControlEvents.TouchDown)
return cell
}
I also tried changing the call to include the ':' -> cell.testBtn.addTarget(cell, action: "buttonTappedOnCell:", forControlEvents: UIControlEvents.TouchDown) but this did not work. Another thing I tried is moving the call function into the view controller and having the target action point to self. This unfortunately did not work either. A quick example of this working would be great. At the end of day would like to be able to have a custom table view cell that contains 6 radio buttons allowing the user to select just one. But baby steps first at this point. Thanks for any help.
I think the issue is caused by the event you specified in the code.
cell.testBtn.addTarget(cell, action: "buttonTappedOnCell", forControlEvents: UIControlEvents.TouchDown)
Change that ToouchDown to TouchUpInside
cell.testBtn.addTarget(cell, action: "buttonTappedOnCell", forControlEvents: UIControlEvents.TouchUpInside)
Also why are you setting it programmaticaly ? From your code I think you have the button on your storyboard. So why should you add the IBAction programmaticaly ? (Since the action is also invoked on the same class).
Declare your buttonTappedOnCell as an IBAction and connect it directly to your button on storyboard/xib.

Resources