Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7414385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:05:31+00:00 2026-05-29T07:05:31+00:00

im working on a student project where I need to know which button in

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:05:31+00:00Added an answer on May 29, 2026 at 7:05 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working over one my student project over .Net4 and MVC3. Project is
I'm a student working on a project where a need to integrate my iPad
I am working on a project in which I have to develop bio-passwords based
I'm working on a implementing a SNMP into a GWT project. I need to
i am currently working on one project. In my project there are many entity/POJO
I'm working on a student project team building application. I'm familiar with optimization but
I am a university student working on a research project that involves migrating a
My student group and I are trying to continue working on a project we
I'm about to start working on a rich-internet-application project for a student organization at
I'm working on a project where I need to generate end-user documentation for a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.