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