ios - Scrolling in UITableView makes UIImages appear on cells where they do not exsist -


i have strange problem uitableview. add checkmark image cells not have date property set current day. when tableview populated seems working fine since first elements, has no date, not show checkmark. when scroll down first cell correct date, checkmark showed. when scroll up, checkmark showed in cells. why happen?

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {     static nsstring *cellidentifier = @"inventoryingsearchresultcell";     uitableviewcell *cell = [self.tableview dequeuereusablecellwithidentifier:cellidentifier];     if (cell == nil) {         cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylevalue1 reuseidentifier:cellidentifier];         cell.accessorytype = uitableviewcellaccessorynone;         cell.selectionstyle = uitableviewcellselectionstyledefault;         cell.backgroundcolor = [uicolor clearcolor];          uiview *selectioncolor = [[uiview alloc] init];         selectioncolor.backgroundcolor = myappdelegate.config.apptheme;         cell.selectedbackgroundview = selectioncolor;          // create accessory view         uiimage *image = [uiimage imagenamed:@"u12_normal.png"];         cell.accessoryview = [[uiimageview alloc] initwithimage:image];     }      // fill in data     nsdictionary *item = [searchresult objectatindex:indexpath.row];     nsstring *inventorydate;     inventorydate = [item valueforkey:@"inventorydate"];     if (![inventorydate isequal:[nsnull null]]){         nsdateformatter *dateformat = [[nsdateformatter alloc] init];         [dateformat setdateformat:@"yyyy-mm-dd't'hh:mm:ss"];         nsdate *date = [dateformat datefromstring:inventorydate];          if ([self hasbeeninventoried:date]) {             uiimage *image1 = [uiimage imagenamed:@"tick.png"];             uiimageview *view1 = [[uiimageview alloc] initwithframe:cgrectmake(16, 40, 16, 16)];             view1.image = image1;             [cell.accessoryview addsubview:view1];         }     } else {         inventorydate = @"";     }     return cell; } 

it's happening because you're reusing cells. in event checkmark needs shown, you're adding cell. however, when not required, you're not setting image blank/nil.

when cell reused has image on it, never tell remove checkmark.

i recommend adding view1 accessory subview when cell nil subview exists. then, if inventory requires checkmark, set image. if not require checkmark, set image nil.


side note: in general, add subviews reusable cell when cell nil. if add subviews existing cells, there's chance you'll end having multiples of subview on cell.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -