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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:14:34+00:00 2026-06-15T18:14:34+00:00

I have a code for a table view with a cell that i created.

  • 0

I have a code for a table view with a cell that i created. Each cell holds three buttons. Each button should have an image as a background image.

This is my code for the CellForRowArIndex:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"CellView" owner:self options:nil];
        cell = nibLoadedCell;
    }

    for (int i = 1; i < 4; i++) {
        //----- select the correct button from the cell
        button = (UIButton *)[cell viewWithTag:(i)];


        //----- cleaning the button, drawing it's corners, borders and giving it an action
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.borderWidth = 2;
        button.layer.cornerRadius = 5;
        button.clipsToBounds = YES;


        //------ Setting up the btnImg in order to place on the buttons (if is for checking i'm not taking pictures out of the array bounds)
        UIImage *btnImg = [[UIImage alloc] init];
        if (((indexPath.row * 3) + i - 1) < photosArray.count) {
            btnImg = [photosArray objectAtIndex:((indexPath.row * 3) + i - 1)];

            //NSLog(@"I am at indexpath.row: %d",indexPath.row);
            //NSLog(@"I show image number %d in the array",[photosArray indexOfObject:btnImg]);
            NSLog(@"Expression is: %d",((indexPath.row * 3) + i - 1));
        }


        //------ Add btnImg to the button
        [button setBackgroundImage:btnImg forState:UIControlStateNormal];



        //------ Giving special tags for each button in order to recognize it later
        button.tag = imageButtonTag;
        imageButtonTag--;
    }

    return cell;
}

As you can see, the for loop in my code is in charge of taking the images from my “photosArray” and placing them in the correct button in the cell.

The issue:
The first ~23 buttons get the correct images. Afterwards, (requires to scroll down a bit) starting image 24, the images start repeating. Image 24 is equal to image 0. Image 25 equals image 1 and so on…

I checked many things already. Everything looks fine! I even checked the photosArray indexOfObject and the numbers are correct.

You might think the array is made this way. I thought so too, but i assure you it’s fine. I deleted my for loop and instead entered the following code:

//----- select the correct button from the cell

button = (UIButton *)[cell viewWithTag:(1)];
 //----- cleaning the button, drawing it's corners, borders and giving it an action
[button setBackgroundImage:nil forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.layer.borderWidth = 2;
button.layer.cornerRadius = 5;
button.clipsToBounds = YES;

//------ Setting up the btnImg in order to place on the buttons
UIImage *btnImg = [photosArray objectAtIndex:indexPath.row];

//------ Add btnImg to the button
[button setBackgroundImage:btnImg forState:UIControlStateNormal];

This gives me a tableView with one button in each cell and the buttons get the right images…

One last thing I tried, is this code in my previous code right after creating btnImg:

if ([btnImg isEqual:[photosArray objectAtIndex:0]]) {
    NSLog(@"Match found at: %d",[photosArray indexOfObject:btnImg]);
}

But nothing matches (although when i run it i see the images are the same).

This issue is driving me insane for about a week now and i would really appreciate it if someone could help me solve it.

Thanks!

  • 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-15T18:14:35+00:00Added an answer on June 15, 2026 at 6:14 pm

    Resetting of view tag is causing it not to be identified. Better way for you to identify each control is through subclassing or some other strategy, especially since you are using tags to get to the control

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

Sidebar

Related Questions

I have a table that has a column that contains links. Each cell in
I have three UITableViews that should look identical. Each is placed inside a different
I have created a five column (text boxes) cell (row) in table view controller
I have a nib-based table view cell which I created in Interface builder. I
I have a table view. I have created a method that takes all the
I have some code that shows results in a table Right now it will
I have those code snippet that checks the fans table and gets everyone who
I have some code to update a database table that looks like try {
I have a custom cell and i displayed that custom cell in a table
I have a UITableView and a UITableCell subclass. Each table cell has two scrollviews

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.