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 6173305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:32:56+00:00 2026-05-23T23:32:56+00:00

I have this code on my cellForRowAtindexPath, with a custom cell and a button

  • 0

I have this code on my cellForRowAtindexPath, with a custom cell and a button on the each cell.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
 static NSString *MyIdentifier = @"tblCellView";

 TableCellView *cell = (TableCellView *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

 Alarms *alarma = (Alarms *)[alarmsArray objectAtIndex: indexPath.row];

 if (!cell) {
  [[NSBundle mainBundle] loadNibNamed:@"TableCellView" owner:self options:nil];
  cell = tblCell;

  UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(-7, 4, 30, 33)];

  [button addTarget:self action:@selector(favButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  [button setTag:1];
  [cell.contentView addSubview:button];

  [button release];
 }

 // Set up the cell.
 [cell setLabelText:  [alarma nombreAlarma]];

 UIButton *button = (UIButton *)[cell viewWithTag:1];

 button.tag = indexPath.row;

 return cell;
}

My problem is that when I click on the button, I got random results, as soon as I move on the table and cells are reusing I got different indexes that are not equal to label tex for the cell in question.


-(IBAction)favButtonAction: (id)sender
{
 UIButton *button = (UIButton *)sender; 

 Alarms *alarma = (Alarms *)[alarmsArray objectAtIndex: button.tag];

 NSLog(@"labelText: %@",[alarma nombreAlarma]);

}

for example, the first cell always is ok but the last cell is equal to first objectAtIndex (maybe button.tag is equal to 0 when it must be 14?)

  • 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-23T23:32:56+00:00Added an answer on May 23, 2026 at 11:32 pm

    Don’t put tag value for button inside cell.

    This is my button inside the content view of my cell. Note: button should be subview of Contentview.

    [cell1.YourButton addTarget:self action:@selector(OnClickCategory:) forControlEvents:UIControlEventTouchUpInside];
    

    Action for my button

    - (void) OnClickCategory:(id) sender {
    
    //NSIndexPath *indexPath =(NSIndexPath *)[sender tag];
    
    UIView *senderButton = (UIView*) sender;
    NSIndexPath *indexPath = [CategoryTable indexPathForCell: (UITableViewCell*)[[senderButton superview]superview]];
    
    NSLog(@"%d",indexPath.row);
    

    }

    This works for me.

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

Sidebar

Related Questions

I have this in my tableView:cellForRowAtIndexPath: if (condition) { // make a custom cell
I'm reading a custom table cell in tableView:cellForRowAtIndexPath: from a nib file. This works
UITableView custom cell showing weird results? I have my code below. Everything was showing
I have used custom cell class for my uitableview and used in cellForRowAtIndexPath method.
I have created a custom UITableViewCell , but when I dequeue the cell, sometimes
hi i am working on UITableView custom cell for twitter, for each cell i
I have a UITableView put in edited mode. self.tableView.editing = YES; In this table
I have a UITableView with about 20 cells, in each cell there are three
This is my code (from a Core Data tutorial): [eventsArray insertObject:event atIndex:0]; NSIndexPath *indexPath
I have this code in jQuery, that I want to reimplement with the prototype

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.