Ping
Ping

Reputation: 349

UIButton not clickable in UITableViewCell

I added multiple buttons in my UITableViewCell wrapped with a toolbar, but all of them are not clickable, once I drag a button to the outside of the table view it's clickable already.

Here is the screenshot of my sample app and scene:

enter image description here

Button 1 is not clickable but button 2 is clickable, User Interaction Enabled has been ticked.

Upvotes: 1

Views: 4259

Answers (3)

Anand
Anand

Reputation: 71

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

cell.button.tag = indexPath.row
cell.button.addTarget(self, action: "btnClicked:", forControlEvents: .TouchUpInside)

}

Upvotes: 0

Ping
Ping

Reputation: 349

Okay found the issue already, was a mistake by my own, it's because I set the tableViewCell "User Interaction Enabled" to NO, cause I want to disable the Table View default row selection.

So I need to set each layers of view "User Interaction Enabled" to YES, then the button is now clickable, thanks for all the replies!

Upvotes: 1

Ty Lertwichaiworawit
Ty Lertwichaiworawit

Reputation: 2950

Create you button in viewForHeaderInSection, like this..

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
    [sectionView addSubview:btn];

    //add other subviews to section viewww..

    //return
    return sectionView;
}

- (void) btnClicked
{
    //do your thing here..
}

Upvotes: 0

Related Questions