ios - Adding subview to `UITableViewCell` is repeated -


i use custom edit actions edit row indexpath after action pressed

-(nsarray *)tableview:(uitableview *)tableview editactionsforrowatindexpath:(nsindexpath *)indexpath {      uitableviewrowaction *deletebutton = [uitableviewrowaction rowactionwithstyle:uitableviewrowactionstyledefault title:@"delete" handler:^(uitableviewrowaction *action, nsindexpath *indexpath)     {       uitableviewcell *cell = (uitableviewcell*)[tableview cellforrowatindexpath:indexpath];       uiswitch *switch = [[uiswitch alloc] initwithframe: cgrectmake(0,0,0,0)];       [cell.contentview addsubview:switch];     }];   return @[deletebutton]; } 

this adds uiswitch cell pressed delete action button, however it's being repeated every 12 rows or so; indexpaths different (0-1)-(0-2)..etc.

i believe caused because of method used grab cell in line
uitableviewcell *cell = (uitableviewcell*)[tableview cellforrowatindexpath:indexpath];

by design, cell's re-used cells scroll off top of screen , fresh cells scroll bottom. unless have opted out of behavior, explain precisely you're observing. if set property on cell visible (be text on label, or adding subview you're doing), changes need un-done in order when cell "re-used" or recycled, modifications aren't still visible in context you're not expecting or desiring.

one solution problem use custom subclass of uitableviewcell instead of adding switch subview standard uitableviewcell. give custom subclass property reference subview switch you're adding in rowaction method. reference gives ability remove or hide later.

you might consider making switch subview present in custom cell's contentview, hidden or shown needed action method.

in subclass, override -prepareforreuse method, , either remove or hide subview custom cell's content view prepare cell presented in fresh context (e.g. without added switch when not expecting it.)

create custom cell subclass:

@interface samplecelltableviewcell : uitableviewcell  @property (weak, nonatomic) uiswitch *aswitch;  @end  @implementation samplecelltableviewcell  - (void)prepareforreuse {     [[self aswitch] removefromsuperview];     [self setaswitch:nil]; }  @end 

in action method, assign property:

-(nsarray *)tableview:(uitableview *)tableview editactionsforrowatindexpath:(nsindexpath *)indexpath {      uitableviewrowaction *deletebutton = [uitableviewrowaction rowactionwithstyle:uitableviewrowactionstyledefault title:@"delete" handler:^(uitableviewrowaction *action, nsindexpath *indexpath)     {       sampletableviewcell *cell = (sampletableviecell*)[tableview cellforrowatindexpath:indexpath];       uiswitch *switch = [[uiswitch alloc] initwithframe: cgrectmake(0,0,0,0)];       [cell.contentview addsubview:switch];       [cell setaswitch:switch];     }];   return @[deletebutton]; } 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -