質問

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 :)!

役に立ちましたか?

解決

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!

他のヒント

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.

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

If you think otherwise, show some code.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top