简体   繁体   中英

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

我使用的是自定义tablecell ,我希望该cell内有两个按钮动作,一个用于pushViewController ,一个用于popViewControllerAnimated如何实现?

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
{

}

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

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

}

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM