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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:08:59+00:00 2026-06-16T10:08:59+00:00

Hello everyone, I am trying to add custom button with tag related to indexPath.row.

  • 0

Hello everyone,
I am trying to add custom button with tag related to indexPath.row. I can view the tag value correctly if I don’t insert new row into tableview. But, when I insert new row, my tag value of new row is not correct if the inserted row is not within 0 to 9 (iphone 5 can show up to that). On iphone, I need to scroll down to see.

However, with the same code, I can get my tag value correctly on my ipad. I don’t need to scroll down table view on my ipad to see all my row.I would like to know why it happen and how to solve.

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

    static NSString *CellIdentifier = @"dialoguesTableCell";
    dialoguesCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[dialoguesCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:CellIdentifier];

    }

    UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [yourButton setImage:[UIImage imageNamed:@"1StarBlank.png"]     forState:UIControlStateNormal];
    [yourButton setTitle:@"Button" forState:UIControlStateNormal];
    [yourButton addTarget:self action:@selector(buttonSelected:)   forControlEvents:UIControlEventTouchUpInside];
    yourButton.tag=indexPath.row;
    yourButton.frame = CGRectMake(5, 10, 40, 25);
    [cell addSubview:yourButton];
    return cell;


}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath    *)indexPath
{
   NSIndexPath *indexPathstoinsert = [NSIndexPath indexPathForRow:indexPath.row+1     inSection:section];
   NSArray *indexPathsToInsertArray = [NSArray arrayWithObject:indexPathstoinsert];
   [[self mainTableView] insertRowsAtIndexPaths:indexPathsToInsertArray withRowAnimation:UITableViewRowAnimationRight];

}
  • 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-06-16T10:09:00+00:00Added an answer on June 16, 2026 at 10:09 am

    this is not working correctly because the UITableView reuses cells

    when you scroll down, the first cells arent visible anymore and are reused.

    if the table is .. ‘small’ you can just work around it by not reusing cells
    BUT if the table isnt just a few entries but a lot of data you really want to change your way

    assign the tag for the button in:
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

    e.g.

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        UIButton *b = nil;
        for(UIView *v in cell.subviews) {
            if([v isKindOfClass:[UIButton class]]) {
                b = (UIButton*)v;
                break;
            }
        }
    
        b.tag = indePath.row;
    }
    

    in the commment you mentioned another problem: the buttons are hidden in your didSelectRow method and are then gone from other cells too after scrolling. SAME issue: the cell objects are REUSED by the tableview. dont store state in reusable cells!

    Instead have a ‘model’ tree or array that remembers the state : text, image, tag, hide:yes/no

    NSArray *myTableContents

     NSMutableDictionary *d1 = [@{@"text:@"bla", @"hidden":@NO} mutableCopy];
     NSMutableDictionary *d2 = [@{@"text:@"bloo", @"hidden":@NO} mutableCopy];
     NSMutableDictionary *d3 = [@{@"text:@"foo", @"hidden":@NO} mutableCopy];
     myTableContents = @[d1,d2,d3];
    

    THEN always use THAT array in numberOfRows and in viewForRow and modify it in didSelectEntry

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

Sidebar

Related Questions

Hello everyone I am trying to get an audio tag to work in rails.
hello everyone I'm trying to write function which can unfold int in the list
Hello everyone I'm trying to display my array's values in my View but there
Hello everyone I'm trying to return an array from controller to view. Here is
hello everyone I am trying to make a script that steps through a list
Hello everyone I am trying to resize an image of 700kb with imagecreatefromjpeg. This
Hello everyone I'm trying to work with jboss messaging, does anyone knows the default
Hello everyone I'm trying to improve my Java skills by solving some problems from
Hello everyone I am trying to write a string on image during runtime and
Hello everyone i am trying to implement push notifications with urbanairship i followed the

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.