xcode 如何在 ios 中的自定义表格单元格内创建按钮操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24444245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:08:09  来源:igfitidea点击:

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

iosiphonexcodeuitableview

提问by A.S.K

I'm using a custom tablecelland I want two button action inside that cellone for pushViewControllerand one for popViewControllerAnimatedhow can it be achieved ?

我正在使用自定义,tablecell并且我想要在其中cell一个用于pushViewController和一个用于popViewControllerAnimated如何实现的两个按钮操作?

采纳答案by Rushi

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
{

}

回答by Nitin Gohel

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

}

回答by Vinaykrishnan

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
{

}

回答by User558

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