简体   繁体   English

如何在iOS中的自定义表格单元中创建Button动作

[英]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 : 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: 假设在CellForRowIndex旁边设置为:

- (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 在您的cellForRowAtIndexPath中创建如下

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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