如何在ios中的自定义表格单元格中创建按钮操作
我正在使用一个自定义的如何在ios中的自定义表格单元格中创建按钮操作,ios,iphone,xcode,uitableview,Ios,Iphone,Xcode,Uitableview,我正在使用一个自定义的tablecell,我希望在单元格中有两个按钮动作,一个用于pushViewController,另一个用于popViewControllerAnimated,这是如何实现的?假设在CellForRowIndex旁边设置如下: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CellInviteTa
tablecell
,我希望在单元格中有两个按钮动作,一个用于pushViewController
,另一个用于popViewControllerAnimated
,这是如何实现的?假设在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
}
在自定义单元格中,必须创建两个按钮。在单元格中为行索引路径编写以下代码:
-(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
{
}
在您的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];
}
[cell.btnObject addTarget:self action:@selector(pushME)for controlEvents:UIControlEventTouchUpInside]代码>实现pushMe方法。Pop也一样。谢谢你的帖子,你能详细阐述并发布ans吗?这会很有帮助的。你能展示一下你是如何创建自定义单元格的吗?这样我就知道了,也会帮助你。是的,实现该方法并在该方法中[self.navigationController popViewControllerAnimated:yes]
for pop.cell.back.tag=indexPath.row;[cell.back addTarget:self action:@selector(yourButtonClicked:)for controlEvents:UIControlEventTouchUpInside];。我正在使用它,但是如何在方法中放置按钮操作?
-(void)cellDeleteAction:(UIButton *)sender {
UIButton *button = (UIButton *)sender;
NSString *rateId = [rate_IdArray objectAtIndex:button.tag];
}