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

时间:2014-06-27 04:58:52

标签: ios iphone xcode uitableview

我正在使用自定义tablecell,我希望在cell内有两个按钮操作pushViewController,一个popViewControllerAnimated如何实现?

4 个答案:

答案 0 :(得分:2)

在自定义单元格中,您必须创建两个按钮。在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;
}

将标签设置为按钮非常重要,因为它们会告诉您单击了哪个行按钮。

定义您的行动:

-(void)popButtonClicked:(id)sender
{

}

-(void)pushButtonClicked:(id)sender
{

}

答案 1 :(得分:1)

假设您在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

}

答案 2 :(得分:0)

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
{

}

答案 3 :(得分:0)

在你的cellForRowAtIndexPath中创建如下

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

之后创建按钮操作

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