ios - Custom UITableViewCell prepareForReuse not working as expected -


my prepareforreuse isn't working properly. have uitableview supposed have login uibutton in first row of first section of table only. when, in prepareforreuse, remove login button, stays , comes onto next batch of rows. (video illustrate -> http://pixori.al/8g3v )

here's custom uitableviewcell:

#import "magradecell.h"  @implementation magradecell  - (id)initwithstyle:(uitableviewcellstyle)style reuseidentifier:(nsstring *)reuseidentifier cellforrowatindexpath:(nsindexpath *)indexpath {     self = [super initwithstyle:style reuseidentifier:reuseidentifier];     if (self) {         // initialization code     }     return self; }  -(void)layoutsubviews{     [super layoutsubviews]; }  - (void)prepareforreuse {     self.loginbutton = nil;     [self removefromsuperview];     [self.loginbutton removefromsuperview];     self.textlabel.text = nil;      [super prepareforreuse]; }  /*  - (void)setselected:(bool)selected animated:(bool)animated  {  [super setselected:selected animated:animated];    // configure view selected state  }*/  @end 

and part of viewcontroller sets cells (cellforrowatindexpath). ie put qbflatbutton , everything:

- (magradecell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {     static nsstring *cellidentifier = @"cellidentifier";     //uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier];     magradecell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier];   //    // redefine layout variables in method `viewdidload`     cgfloat inset = 20; // padding       if (! cell) {         cell = [[magradecell alloc] initwithstyle:uitableviewcellstylevalue1 reuseidentifier:cellidentifier cellforrowatindexpath:indexpath];     }       // sets attributes of each cell     cell.selectionstyle = uitableviewcellselectionstylenone;     cell.backgroundcolor = [uicolor colorwithwhite:0 alpha:0.2];     cell.textlabel.textcolor = [uicolor whitecolor];     cell.detailtextlabel.textcolor = [uicolor whitecolor];     qbflatbutton* loginbutton = nil;      if (indexpath.section == 0) {         if (indexpath.row == 0) {             [self configureheadercell:cell title:@"grades"];                  uiview *cellview = cell.contentview;                 loginbutton = [[qbflatbutton alloc] initwithframe:cgrectmake((cellview.frame.size.width - (80 + inset)), 18, 80, (cellview.frame.size.height -(cellview.frame.size.height/2)))];                 [loginbutton addtarget:self action:@selector(loginbuttonwaspressed)forcontrolevents:uicontroleventtouchupinside];                 loginbutton.facecolor = [uicolor graycolor];                 loginbutton.sidecolor = [uicolor clearcolor];                  loginbutton.radius = 6.0;                 loginbutton.margin = 4.0;                 loginbutton.depth = 3.0;                 loginbutton.alpha = 0.3;                  loginbutton.titlelabel.font = [uifont fontwithname:@"helveticaneue-light" size:20];                 [loginbutton settitlecolor:[uicolor blackcolor] forstate:uicontrolstatenormal];                 [loginbutton settitle:@"login" forstate:uicontrolstatenormal];                 [cellview addsubview:loginbutton];         } else {             cell.selectionstyle = uitableviewcellselectionstyleblue;             magradeclient *grade = [[magradeclient alloc] init];             [self configuregradescell:cell grade:grade];         }     } else if (indexpath.section == 1) {         if (indexpath.row == 0) {             [self configureheadercell:cell title:@"hourly forecast"];         }         else {             // hourly weather , configure using method             macondition *weather = [mamanager sharedmanager].hourlyforecast[indexpath.row - 1];             [self configurehourlycell:cell weather:weather];         }     }     else if (indexpath.section == 2) {         if (indexpath.row == 0) {             [self configureheadercell:cell title:@"daily forecast"];         }         else if (indexpath.section == 2) {             // daily weather , configure using method             macondition *weather = [mamanager sharedmanager].dailyforecast[indexpath.row - 1];             [self configuredailycell:cell weather:weather];         }     }      return cell; } 

if do:

self.loginbutton = nil; 

and try do:

[self.loginbutton removefromsuperview]; 

it won't work because nilled reference.

consider using different cell identifier cell isn't pure reuse if adding , removing button. consider hiding / showing button. if want remove change code to:

- (void)prepareforreuse {     [self.loginbutton removefromsuperview];     self.loginbutton = nil;      self.textlabel.text = nil;      [super prepareforreuse]; } 

it looks aren't ever setting:

cell.loginbutton = loginbutton; 

so cell may not have reference use anyway...


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -