CustomCell 中的多个按钮。单击哪行的哪个按钮?

发布于 2025-01-02 20:22:03 字数 3134 浏览 0 评论 0原文

我正在进行一个学生项目,我需要知道单击了哪一行中的哪个按钮。

每一行都有不同的标签,一个喜欢按钮,一个不喜欢按钮。

我确实知道如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

祁梦 2025-01-09 20:22:03

为什么不执行如下操作:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *reuseIdentifier = @"MyCell";
    MyCustomCell *cell = (id)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

    if (cell == nil) {

        cell = [[[MyCustomCell alloc] init] autorelease];

        [cell.firstButton addTarget:self action:@selector(firstButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [cell.secondButton addTarget:self action:@selector(secondButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    }

    [cell.firstButton setTag:indexPath.row];
    [cell.secondButton setTag:indexPath.row];

    // other cell setup...

    return cell;
}

- (void)firstButtonPressed:(id)sender {

    NSInteger cellRow = [sender tag];

    // do things...
}

- (void)secondButtonPressed:(id)sender {

    NSInteger cellRow = [sender tag];

    // do things...
}

这假设您的 UITableView 中只有一个部分,对多个部分执行此操作会稍微复杂一些。

Why not do something like the following:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *reuseIdentifier = @"MyCell";
    MyCustomCell *cell = (id)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

    if (cell == nil) {

        cell = [[[MyCustomCell alloc] init] autorelease];

        [cell.firstButton addTarget:self action:@selector(firstButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [cell.secondButton addTarget:self action:@selector(secondButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    }

    [cell.firstButton setTag:indexPath.row];
    [cell.secondButton setTag:indexPath.row];

    // other cell setup...

    return cell;
}

- (void)firstButtonPressed:(id)sender {

    NSInteger cellRow = [sender tag];

    // do things...
}

- (void)secondButtonPressed:(id)sender {

    NSInteger cellRow = [sender tag];

    // do things...
}

This assumes that you only have one section in your UITableView, it's a bit more complicated to do this with multiple sections.

夕色琉璃 2025-01-09 20:22:03

我认为使用块将是一个很好的方法,仍然使用标签,如 @EllNeal 的解决方案:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *reuseIdentifier = @"MyCell";
MyCustomCell *cell = (id)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

if (cell == nil) {

    cell = [[[MyCustomCell alloc] init] autorelease];

    [cell.button1 addTarget:cell action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.button2 addTarget:cell action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.button1.tag = 1;
    cell.button2.tag = 2;
}

cell.onButton = ^(UIButton *theButton) {
     [self handleButton:theButton indexPath:indexPath];
}

return cell;

然后你的处理程序,你告诉按下哪个按钮将是这样的:

- (void)handleButton:(UIButton *)button indexPath:(NSIndexPath *)indexPath
{
   //Use tag of button to identify which button was tapped, as well as the indexPath
}

你的单元格代码看起来有点像这样:

@interface MyCustomCell
...
- (void)buttonAction:(UIButton *)sender
@property (nonatomic, copy) void (^onButton)(UIButton *button);
...
@end

@implementation
...
- (void)buttonAction:(UIButton *)sender
{
    self.onButton(sender);
}
...
@end

希望这有帮助!

I think using blocks would be a good way to go, still using tags as in @EllNeal's solution:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *reuseIdentifier = @"MyCell";
MyCustomCell *cell = (id)[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

if (cell == nil) {

    cell = [[[MyCustomCell alloc] init] autorelease];

    [cell.button1 addTarget:cell action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.button2 addTarget:cell action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.button1.tag = 1;
    cell.button2.tag = 2;
}

cell.onButton = ^(UIButton *theButton) {
     [self handleButton:theButton indexPath:indexPath];
}

return cell;

Then your handler where you tell which button was pressed would be like this:

- (void)handleButton:(UIButton *)button indexPath:(NSIndexPath *)indexPath
{
   //Use tag of button to identify which button was tapped, as well as the indexPath
}

Your cell code would look a bit like this:

@interface MyCustomCell
...
- (void)buttonAction:(UIButton *)sender
@property (nonatomic, copy) void (^onButton)(UIButton *button);
...
@end

@implementation
...
- (void)buttonAction:(UIButton *)sender
{
    self.onButton(sender);
}
...
@end

Hope this helps!

若言繁花未落 2025-01-09 20:22:03

使用 .tag 属性,它非常可靠且可预测。

如果您不这么认为,请显示一些代码。

Use the .tag property, it's very reliable and predictable.

If you think otherwise, show some code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文