UITableCell AccessoryView: Setting accessoryView equal to UIImageView infinite loop

半腔热情 提交于 2019-12-03 00:25:27

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/34624404/uitablecell-accessoryview-setting-accessoryview-equal-to-uiimageview-infinite-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!