2

How to detect multiple buttons in tableview cell and my doubt is with example i have 3 buttons in cell if i tap on one button that button will change colour and and if i click indexpath.row=1 cell button that button will color also need to change help me

  • "indexpath.row=1 cell button" ,what are you trying to say? – Bista Apr 30 '15 at 09:45
  • i have 3 buttons in each cell if i click the any button that button need to change with in that cell only – Raghavender Reddy Apr 30 '15 at 09:47
  • You have to hard-code buttons into table cell with unique tags and assign each button an action with parameters (button tag, Indexpath.row). – Bista Apr 30 '15 at 09:55
  • Code written below is right to change color on button press. But your issue is about to reuse cell. When you scroll the tables cells reuse so it will initalize with default color which you have set in cellForRowAtIndexPath and your your previous selected color gone. :D – Sandip Patel - SM Apr 30 '15 at 12:09

3 Answers3

1

Assign tag to each button using the combination of section & row..

When method assigned to button get called use '%' & '/' for further manipulation.

Let me know if you have any difficulty in implementing it..

Nilesh Patel
  • 6,318
  • 1
  • 26
  • 40
1

I did like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:MyIdentifier];
    }

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(10.0, 0.0, self.tableView.frame.size.width/4, 40.0);
    button.tag = 100 + indexPath.row*total_buttons_in_a_row;
    [button setTitle:[NSString stringWithFormat:@"%ld",(long)button.tag] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:button];

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button2.frame = CGRectMake(10.0+self.tableView.frame.size.width/4+10.0, 0.0, self.tableView.frame.size.width/4, 40.0);
    button2.tag = 100 + indexPath.row*total_buttons_in_a_row + 1;
    [button2 setTitle:[NSString stringWithFormat:@"%ld",(long)button2.tag] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:button2];

    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button3.frame = CGRectMake(10.0+self.tableView.frame.size.width/4*2+10.0, 0.0, self.tableView.frame.size.width/4, 40.0);
    button3.tag = 100 + indexPath.row*total_buttons_in_a_row + 2;
    [button3 setTitle:[NSString stringWithFormat:@"%ld",(long)button3.tag] forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:button3];

    return cell;
}

-(void)btnClicked:(UIButton *)sender{
    id selectedButton = [self.view viewWithTag:sender.tag];
    if ([selectedButton backgroundColor] == [UIColor redColor]) {
        [selectedButton setBackgroundColor:[UIColor clearColor]];

    }else{
        [selectedButton setBackgroundColor:[UIColor redColor]];
    }
}

total_buttons_in_a_row is an Int. In your case define it in viewDidLoad total_buttons_in_a_row=3

P.S - set the Buttons CGRectMake according to your need.

Bista
  • 7,869
  • 3
  • 27
  • 55
  • Better solution: https://stackoverflow.com/questions/39585638/get-indexpath-of-uitableviewcell-on-click-of-button-from-cell/39585749#39585749 – Bista Feb 12 '21 at 07:09
0

Got to your CustomCell.h & right below #import add this code.

@protocol CustomCellDelegate <NSObject>
- (void)buttonActionwith :(NSIndexPath *)indexPath;
@end

Then Add this code right below @interface CustomCell : UITableViewCell

//Manual Properties
@property (strong, nonatomic) NSIndexPath *buttonIndexPath;

//Delegate
@property (nonatomic, weak) id <CustomCellDelegate> delegate;

The use of this NSIndexPath is to identify which cell user selecting.

Then go to CustomCell.m & in your button IBAction method add this code.

[self.delegate buttonActionwith:self.buttonIndexPath];

The meaning of this line of code is that, when the user TouchUpInside the Button in the cell buttonActionwith delegate method calls, if you set this CustomCellDelegate in your UIViewController where the UITableView is, that delegate method will call inside that ViewController too.

Now go to your MainViewController.h & add CustomCellDelegate & it will look like this after that,

@interface MainViewController : UIViewController <CustomCellDelegate,,UITableViewDataSource,UITableViewDelegate>

when creating the Custom UITableViewCell in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath don't forget to add this line of code before return cell;

//Set Cell Values Here
cell.delegate = self; //Setting delegate to self
cell.buttonIndexPath = indexPath;

The modified - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath will look like this,

static NSString *MyIdentifier = @"MyIdentifier"; //Set Identifier for cell

        CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: MyIdentifier]; //init CustomCell

        if (cell == nil) { //Check cell is nill or not
            NSArray *nib;
            nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell"
                                                owner:self options:nil]; //if cell is nil add CustomCell Xib

            for (id oneObject in nib) if ([oneObject isKindOfClass:[CustomCell class]])
                cell = (CustomCell *)oneObject;
        }

        //Set Cell Values Here
        cell.buttonIndexPath = indexPath

        return cell;

Finally add this method inside MainViewController.m

#pragma mark - SwipeableCellDelegate
- (void)buttonActionwith:(NSIndexPath *)indexPath { //Delegate method
    NSLog(@"Button Clicks at index %ld",(long)indexPath.row);
}

This is how to add a single button inside the Cell. you can use this method to more buttons to the cell.

DilumN
  • 2,889
  • 6
  • 30
  • 44