A.S.K
A.S.K

Reputation: 31

How to create a Button action inside custom table cell in ios

I'm using a custom tablecell and I want two button action inside that cell one for pushViewController and one for popViewControllerAnimated how can it be achieved ?

Upvotes: 2

Views: 6161

Answers (4)

User558
User558

Reputation: 1175

In your cellForRowAtIndexPath create like this

cell.deleteBtn.tag = indexPath.row;
[cell.deleteBtn addTarget:self action:@selector(cellDeleteAction:) forControlEvents:UIControlEventTouchUpInside];

after that create button action

-(void)cellDeleteAction:(UIButton *)sender {
    UIButton *button = (UIButton *)sender;
    NSString *rateId = [rate_IdArray objectAtIndex:button.tag];
}

Upvotes: 0

Vinaykrishnan
Vinaykrishnan

Reputation: 768

In cellForRowAtIndexPath :


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *SimpleTableIdentifier = @"myTableViewCell";

myTableViewCell *cell=(myTableViewCell *)[tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

[cell clearsContextBeforeDrawing];

if (cell==nil)
 {
        NSArray *cellObjects=[[NSBundle mainBundle]loadNibNamed:@"myTableViewCell" owner:self options:nil];

        for (id currentObject in cellObjects)
        {
        if ([currentObject isKindOfClass:[UITableViewCell class]])
                {
                    cell=(myTableViewCell *)currentObject;
                }
            }
}

[cell.btnPlus setTag:indexPath.row];

[cell.btnPlus addTarget:self action:@selector(btnPlus_Click:) forControlEvents:UIControlEventTouchUpInside];

return cell;

}



-(IBAction)btnPlus_Click:(id)sender
{

}

Upvotes: 0

Rushi
Rushi

Reputation: 4500

In your custom cell you'll have to create two buttons. Write following code in your cellForRowAtIndexPath :

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
     CellCustomCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"customCell"];

     cell.btnPop.tag = indexPath.row; 
     [cell.btnPop addTarget:self action:@selector(popButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

     cell.btnPush.tag = indexPath.row; 
     [cell.btnPush addTarget:self action:@selector(pushButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

      return cell;
}

It is important you set tags to your button as they'll tell you which row button is clicked.

Define your actions :

-(void)popButtonClicked:(id)sender
{

}

-(void)pushButtonClicked:(id)sender
{

}

Upvotes: 2

Nitin Gohel
Nitin Gohel

Reputation: 49710

Suppose in side to CellForRowIndex you are setting like:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        CellInviteTableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"CellInviteTableViewCell"];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.back.tag = indexPath.row; 
       [cell.back addTarget:self action:@selector(yourButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

 return cell;

}


-(void)yourButtonClicked:(UIButton*)sender
{
     NSLog(@"button tapped Index %d",sender.tag);
    //here you get its each button action you can identirire which button click by its tag

}

Upvotes: 1

Related Questions