Objective c UITableViewCell中的两个按钮:如何判断选择了哪个indexPath?
我想在每个表视图单元格中放置两个按钮。当我点击第一个按钮时,我希望应用程序显示一条警告消息:“你在indexpath:3,0点击了按钮1”。我的问题是:如何在表格视图单元格中放置按钮?有人能指导我吗?当UITableCell中只有一个按钮时,使用indexPath作为标记值是可以的,但如果您想跟踪更多按钮,可以使用模运算符:Objective c UITableViewCell中的两个按钮:如何判断选择了哪个indexPath?,objective-c,cocoa-touch,ios,uitableview,Objective C,Cocoa Touch,Ios,Uitableview,我想在每个表视图单元格中放置两个按钮。当我点击第一个按钮时,我希望应用程序显示一条警告消息:“你在indexpath:3,0点击了按钮1”。我的问题是:如何在表格视图单元格中放置按钮?有人能指导我吗?当UITableCell中只有一个按钮时,使用indexPath作为标记值是可以的,但如果您想跟踪更多按钮,可以使用模运算符: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {
...
imageButton1.tag=indexPath.row*2;
[imageButton1 addTarget:self action:@selector(buttonPushed:)
[cell.contentView addSubview:imageButton];
...
imageButton2.tag=indexPath.row*2+1;
[imageButton2 addTarget:self action:@selector(buttonPushed:)
[cell.contentView addSubview:imageButton];
对于选择器,您可以区分按钮并获得如下所示的索引:
-(void) buttonPushed:(id)sender{
UIButton *button = (UIButton *)sender;
switch (button.tag%2) {
case 0: // imageButton1 is pressed
// to reach indexPath of the cell where the button exists you can use:
// ((button.tag-button.tag%2)/2)
break;
case 1: // imageButton2 is pressed
break;
}
- (IBAction)answerButtonAction:(UIButton *)sender {
NSInteger row = sender.tag;
NSInteger column = sender.titleLabel.tag;
// do something
}
示例中有两个按钮,但您可以根据按钮的数量进行调整。接受@wedo的答案并将其简化一点——您基本上需要两条信息:点击的行数和列数(“列”是按钮的顺序)
解决方案1-这是一个不错的解决方案 可以使用
button.tag
和button.titleLabel.tag
将其存储在按钮上。在-tableView:cellforrowatinexpath:
中,您可以执行以下操作:
UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
button0.tag = indexPath.row;
button0.titleLabel.tag = 0; // button #0 (or column 0)
[button0 addTarget:self action:@selector(cellButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button0];
UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
button0.tag = 0;
[button0 addTarget:cell action:@selector(fireAction:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button0];
您的cellButtonAction:
方法如下所示:
-(void) buttonPushed:(id)sender{
UIButton *button = (UIButton *)sender;
switch (button.tag%2) {
case 0: // imageButton1 is pressed
// to reach indexPath of the cell where the button exists you can use:
// ((button.tag-button.tag%2)/2)
break;
case 1: // imageButton2 is pressed
break;
}
- (IBAction)answerButtonAction:(UIButton *)sender {
NSInteger row = sender.tag;
NSInteger column = sender.titleLabel.tag;
// do something
}
解决方案2——更好的解决方案 上面的方法很好,但是有点粗糙。或者,对按钮进行子类化并添加可以保存行和列值的属性可能需要3分钟
@interface IndexPathButton: UIButton
// NSIndexPath provides a convenient way to store an integer pair
// Note we are using cellIndex.section to store the column (or button #)
@property (strong, nonatomic) NSIndexPath *cellIndex;
@end
@implementation IndexPathButton
@end
您将以与上一个解决方案大致相同的方式使用它,但将值存储在自定义属性而不是标记中。在tableView:cellforrowatinexpath:
// You'd create a button for each column here
IndexPathButton *button0 = [IndexPathButton buttonWithType:UIButtonTypeCustom];
button0.indexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
[button0 addTarget:self action:@selector(cellButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button0];
解决方案3-最佳解决方案
UITableViewCells
通常应在需要完成的任何繁重工作中使用委派。此模式与Apple自己的单元格委托模式最为匹配,例如,tableView:didSelectRowAtIndexPath
和friends。因此,让我们创建一个tableViewCell基类,它可以用来处理任意数量的控件,并且不需要传递索引路径
/** Simple protocol to allow a cell to fire any type of action from a control. */
@protocol SOTableViewCellActionDelegate <NSObject>
@required
-(void)tableViewCell:(UITableViewCell *)cell didFireActionForSender:(id)sender;
@end
@interface SOActionCell : UITableViewCell
@property (nonatomic, weak) id<SOTableViewCellActionDelegate> delegate;
@end
@implementation SOActionCell
-(void)fireAction:(id)sender
{
[self.delegate tableViewCell:self didFireActionForSender:sender];
}
@end
然后在tableViewController中实现所需的委托方法:
-(void)tableViewCell:(UITableViewCell *)cell didFireActionForSender:(id)sender
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSAssert(indexPath, @"indexPath of cell shall always be found."];
if (!indexPath)
return;
// do whatever you want to do with your button action here
// using indexPath, sender tag, button title, etc.
}
已经1.5年了。如果答案对您有帮助,请将其标记为已接受。感谢您的解决方案。小的更正,应该是button0.cellIndex。