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
How to create a Button action inside custom table cell in ios
提问by A.S.K
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 ?
我正在使用自定义,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];
}