CustomCell 中的多个按钮。单击哪行的哪个按钮?
我正在进行一个学生项目,我需要知道单击了哪一行中的哪个按钮。
每一行都有不同的标签,一个喜欢按钮,一个不喜欢按钮。
我确实知道如何使用 NSIndexPath.row 来检测按下了哪一行,但在这种情况下,我需要知道按下了哪一行以及该行中的哪个按钮。
我已经到处寻找一个好的解决方案来检测当每行有多个按钮时单击了哪一行按钮。
根据我阅读其他帖子的理解,使用 button.tag 可能非常不可预测,因此如果可能的话,我宁愿使用不同的方法。
我见过很多应用程序都实现了这一点,所以必须有一个最佳的方法来做到这一点。有人有什么好的建议吗?
代码:
SearchCell.h
@interface SearchCell : UITableViewCell {
IBOutlet UIButton *likebutton2;
IBOutlet UIButton *dislikebutton2;
}
@property (nonatomic,retain) IBOutlet UILabel *track_label;
@property (nonatomic,retain) IBOutlet UILabel *artist_label;
@property (nonatomic,retain) IBOutlet UILabel *album_label;
@property (nonatomic,retain) IBOutlet UIButton *likebutton2;
@property (nonatomic,retain) IBOutlet UIButton *dislikebutton2;
@property (nonatomic, copy) void (^onButton)(UIButton *button);
- (void)buttonAction:(UIButton *)sender;
@end
SearchCell.m
#import "SearchCell.h"
#import "RootViewController.h"
@implementation SearchCell
@synthesize likebutton2, dislikebutton2, track_label, artist_label, album_label;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code.
}
return self;
}
- (void)buttonAction:(UIButton *)sender
{
self.onButton(sender);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state.
}
- (void)dealloc {
[super dealloc];
}
@end
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *uniqueIdentifier = @"searchCell";
SearchCell *cell = nil;
Search *currentSearch = nil;
cell = (SearchCell *) [self.tableView dequeueReusableCellWithIdentifier:uniqueIdentifier];
if (tableView == [[self searchDisplayController] searchResultsTableView]) //Just from some previous debugging
{
currentSearch = [[searchxmlParser searchhits] objectAtIndex:indexPath.row];
}
if(!cell)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SearchCell" owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[SearchCell class]]) {
cell = (SearchCell *)currentObject;
[cell.likebutton2 addTarget:cell action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.dislikebutton2 addTarget:cell action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
cell.likebutton2.tag = 1;
cell.dislikebutton2.tag = 2;
break;
}
}
}
cell.onButton = ^(UIButton *theButton) {
[self handleButton:theButton indexPath:indexPath];
}
cell.track_label.text = [currentSearch track];
cell.artist_label.text = [currentSearch artist];
return cell;
}
感谢您的帮助:)!
im working on a student project where I need to know which button in which row is clicked.
Each row has different labels, one like button and one dislike button.
I do know how to use NSIndexPath.row to detect which row is pressed, but In this case I need to know both which row and which buttton in that row is pressed.
I have searched all over SO for a good solution to detecting which row a button is clicked when having multiple buttons per row.
From what I understand reading the other posts, using button.tag can be very unpredictable so I rather use a different method if possible.
I have seen many apps that have this implemented, so there has to be an optimal way to do this. Does anyone have any good suggestions?
Code:
SearchCell.h
@interface SearchCell : UITableViewCell {
IBOutlet UIButton *likebutton2;
IBOutlet UIButton *dislikebutton2;
}
@property (nonatomic,retain) IBOutlet UILabel *track_label;
@property (nonatomic,retain) IBOutlet UILabel *artist_label;
@property (nonatomic,retain) IBOutlet UILabel *album_label;
@property (nonatomic,retain) IBOutlet UIButton *likebutton2;
@property (nonatomic,retain) IBOutlet UIButton *dislikebutton2;
@property (nonatomic, copy) void (^onButton)(UIButton *button);
- (void)buttonAction:(UIButton *)sender;
@end
SearchCell.m
#import "SearchCell.h"
#import "RootViewController.h"
@implementation SearchCell
@synthesize likebutton2, dislikebutton2, track_label, artist_label, album_label;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code.
}
return self;
}
- (void)buttonAction:(UIButton *)sender
{
self.onButton(sender);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state.
}
- (void)dealloc {
[super dealloc];
}
@end
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *uniqueIdentifier = @"searchCell";
SearchCell *cell = nil;
Search *currentSearch = nil;
cell = (SearchCell *) [self.tableView dequeueReusableCellWithIdentifier:uniqueIdentifier];
if (tableView == [[self searchDisplayController] searchResultsTableView]) //Just from some previous debugging
{
currentSearch = [[searchxmlParser searchhits] objectAtIndex:indexPath.row];
}
if(!cell)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SearchCell" owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[SearchCell class]]) {
cell = (SearchCell *)currentObject;
[cell.likebutton2 addTarget:cell action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.dislikebutton2 addTarget:cell action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
cell.likebutton2.tag = 1;
cell.dislikebutton2.tag = 2;
break;
}
}
}
cell.onButton = ^(UIButton *theButton) {
[self handleButton:theButton indexPath:indexPath];
}
cell.track_label.text = [currentSearch track];
cell.artist_label.text = [currentSearch artist];
return cell;
}
Thank you for helping :)!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不执行如下操作:
这假设您的 UITableView 中只有一个部分,对多个部分执行此操作会稍微复杂一些。
Why not do something like the following:
This assumes that you only have one section in your
UITableView
, it's a bit more complicated to do this with multiple sections.我认为使用块将是一个很好的方法,仍然使用标签,如 @EllNeal 的解决方案:
然后你的处理程序,你告诉按下哪个按钮将是这样的:
你的单元格代码看起来有点像这样:
希望这有帮助!
I think using blocks would be a good way to go, still using tags as in @EllNeal's solution:
Then your handler where you tell which button was pressed would be like this:
Your cell code would look a bit like this:
Hope this helps!
使用
.tag
属性,它非常可靠且可预测。如果您不这么认为,请显示一些代码。
Use the
.tag
property, it's very reliable and predictable.If you think otherwise, show some code.