Friday 15 June 2012

ios - UIImageView added to cell contentView displaying randomly when scrolling through UITableView -


I UITableView is pulling data from an array. Items in this array have a property called IISelected. I am trying to put a UIImageView for the cell contentView each item is selected.

UITableView but when sales again being to use the cells to my image re-use should not do it. I do not know about my life how should I do it differently? I've attached a screen shot showing this issue. If I scroll up and image went everywhere Hold down:

  - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath {static NSString * SchoolCellIdentifer = @ "SchoolCellIdentifier"; School InfoTime * Item = [auto. SchoolsAbstract Object Index: IndexPath.ro]; UITableViewCell * cell = [table view dequeueReusableCellWithIdentifier: SchoolCellIdentifer]; If (cell == null) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: SchoolCellIdentifer]; Cell.contentView.backgroundColor = [BVColors WebDarkBlue]; } Cell.textLabel.text = item.Name; If the ((item. Select, Iwvltostring: @ "1"]) {cell.contentView.backgroundColor = [Bivisielors Vebblu]; UIImageView * selectedItemCheckMarkIcon = [[UIImageView alloc] initWithFrame: CGRectMake (300, 13, 17, 17.5 )] [elected ITemCheckMarkIcon set image: [UIImage imageNamed: @ "check-mark.png"]]; [Cell.contentView addSubview: selected ITemCheckMarkIcon];} Else {cell.contentView.backgroundColor = [BVColors WebDarkBlue]; } Return Cell;}   

Enter image details here

You must ensure that the UIImageView is removed from the cell code view It appears in your code when cell is deployed that the image view is still in the visual hierarchy of cells.

The best solution is to keep your cell hold in context of the image view and / P>

Do the following:

  if ([item selected EEqualToString: @ "1"]) {cell.contentView.backgroundColor = [BVColors WebBlue ]; Cell.myImageView = [[UIImageView alloc] initWithFrame: CGRectMake (300, 13, 17, 17.5)]; [Selected ITemCheckMarkIcon set image: [UIImage imageNamed: @ "check-mark.png"]]; [Cell.contentView addSubview: cell.myImageView]; } And {{cell.myImageView removeFromSuperview]; Cell.myImageView = Zero; Cell.contentView.backgroundColor = [BVColors WebDarkBlue]; }   

Note the removal of the image view.

No comments:

Post a Comment