iosuitableviewuiimageviewaccessoryview

UITableCell AccessoryView: Setting accessoryView equal to UIImageView infinite loop


EDIT: I have figured out the answer on my own but here it is for anyone else who needs it: UIImageViews cannot be shared so a different instantiation of each UIImageView is required for each visible cell. Now you know.

I have a custom table that has 2 types of cells. One cell is just set to toggle between a normal accessory of type checkmark. Another cell is set to have a custom image as the accessory type. When selected that accessory image changes to its opposite type, showing an "Invited" or "Invite" message.

I've narrowed down the code at fault to the following, found within my tableView:cellForRowAtIndexPath delegate method.

if(indexPath.section == 0){
    cell = [tableView dequeueReusableCellWithIdentifier:self.directCellID];
    cellValue = [self.contactsUsingApp objectAtIndex:indexPath.row];
    cell.imageView.image = [self getContactImage:indexPath.row];
//vvvvvvvvvvvvvvvvv This is the section at fault vvvvvvvvvvvvvvvvv
    if([self.selectedContactsUsingApp containsObject:indexPath])
        cell.accessoryView = self.invitedStatus;
    else
        cell.accessoryView = self.notInvitedStatus;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
}

If I comment out that section I no longer have runaway memory usage (the Simulator showed me that there was some sort of constant allocation going on, it got up passed 1.29Gb after starting from 40Mb) but, obviously, the images no longer show.

If it matters the UIImageViews are initialized as follows:

UIImage *invite = [self imageWithImage:[UIImage imageNamed: @"invite_btn.png"] scaledToSize:CGSizeMake(40, 20)];
UIImage *invited = [self imageWithImage:[UIImage imageNamed: @"invited_btn.png"] scaledToSize:CGSizeMake(40, 20)];
self.notInvitedStatus = [[UIImageView alloc]initWithImage:invite];
self.invitedStatus = [[UIImageView alloc]initWithImage:invited];

(imageWithImage:scale is a function that returns a resized image to the appropriate scale accounting for retina found here: The simplest way to resize an UIImage?)

The same freezing happens when I select one of the cells because my tableView:didSelectRowAtIndexPath method works by the same toggling logic as the initialization method.

Help?


Solution

  • I have figured out the answer on my own but here it is for anyone else who needs it: UIImageViews cannot be shared so a different instantiation of each UIImageView is required for each visible cell. Now you know.