ios - dequeueReusableCellWithIdentifier and custom cells -


ok, i’ve got table 3 different prototype cells (cellvaccination, celladmin, cellexpire). in cellforrowatindexpath method, i’m splitting core data object across 3 individual cells structurally table following:

- drug 1   - drug 1 admin   - drug 1 expire - drug 2   - drug 2 admin   - drug 2 expire - drug 3   - drug 3 admin   - drug 3 expire 

additionally, i’ve programmatically added uiswitch ‘top level’ cell (i.e. drug 1) switch might control secondary cells features (i.e. color, text, etc). here current cellforrowatindexpath looks like:

- (vaccinetableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {      // need adjust indexpath because split single core data object 3 different rows     nsindexpath *adjustedindexpath = [nsindexpath indexpathforrow:indexpath.row / 3 insection:indexpath.section];     vaccine *vaccine = [self.fetchedresultscontroller objectatindexpath:adjustedindexpath];      // define switch added primary table rows     uiswitch *switchview = [[uiswitch alloc] initwithframe:cgrectzero];      if (indexpath.row % 3 == 0) {         static nsstring *cellidentifier = @"cellvaccination";         vaccinetableviewcell *cell = [mytableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath];         cell.vaccinename.text = vaccine.vaccinename;          // add switch table row         cell.accessoryview = switchview;         [switchview addtarget:self action:@selector(switchchanged:) forcontrolevents:uicontroleventvaluechanged];         switchview.tag = indexpath.row;         switchview.on = [vaccine.vaccineenabled boolvalue];          // problem area below         if (switchview.on) {             vaccinetableviewcell *cell1 = [mytableview dequeuereusablecellwithidentifier:@"celladmin" forindexpath:[nsindexpath indexpathforitem:indexpath.row + 1 insection:0]];             cell1.vaccineadmin.textcolor = [uicolor redcolor];             cell1.vaccineadmindate.textcolor = [uicolor redcolor];              nslog(@"row %d %@", indexpath.row, switchview.on ? @"on" : @"off");         } else {             vaccinetableviewcell *cell1 = [mytableview dequeuereusablecellwithidentifier:@"celladmin" forindexpath:[nsindexpath indexpathforitem:indexpath.row + 1 insection:0]];             cell1.vaccineadmin.textcolor = [uicolor lightgraycolor];             cell1.vaccineadmindate.textcolor = [uicolor lightgraycolor];              nslog(@"row %d %@", indexpath.row, switchview.on ? @"on" : @"off");          }             //          return cell;     }      else if (indexpath.row % 3 == 1) {         vaccinetableviewcell *cell = [mytableview dequeuereusablecellwithidentifier:@"celladmin" forindexpath:indexpath];         cell.vaccineadmindate.text = vaccine.vaccineadmin;         return cell;     }      else if (indexpath.row % 3 == 2) {         vaccinetableviewcell *cell = [mytableview dequeuereusablecellwithidentifier:@"cellexpire" forindexpath:indexpath];         cell.vaccineexpiredate.text = vaccine.vaccineexpire;         return cell;     }      else {         // nothing @ moment     } } 

the problem i’m having seems stem around area notated within “problem area below” element, more i’m guessing dequeuereusablecellwithidentifier. in theory, what’s supposed happen when cells first populated via core data objects, want test whether or not switch either “on” or “off” , adjust parameter (such color) appropriately that, without other interaction, respective rows colored appropriately.

what’s happening - let’s assume i’m simulating on iphone 4s , screen displaying 4 row sets, or 12 rows total (4 rows of 3 different prototypes). , let’s assume first 2 switched on , second 2 switched off, again driven directly core data. initially, screen correct, first 2 items have been colored red, , next 2 items have been colored gray. when start scrolling table up, next 2 (that off screen) colored red, , pattern continues. oddly, when nslog returns row identifiers (seen within “problem area” section) looks it’s identifying correct rows, apparently it’s not, i.e.:

vaccinations[10952:1486529] row 0 on vaccinations[10952:1486529] row 3 on vaccinations[10952:1486529] row 6 off vaccinations[10952:1486529] row 9 off vaccinations[10952:1486529] row 12 off vaccinations[10952:1486529] row 15 off 

i believe has dequeuereusablecellwithidentifier method, why nslog identify rows correctly, changing of colors not hit correct rows?

you have references cell1 in dequeue cell different nsindexpath, configure color of cell, , discard cell. i'm guessing trying adjust visual appearance of different cell (the next cell).

that not correct. cellforrowatindexpath should adjusting state of current cell only. if want adjust appearance of celladmin cell, should within if (indexpath.row % 3 == 1) ... block.

so if (indexpath.row % 3 == 0) block in model determine if switch on or off. if (indexpath.row % 3 == 1) block in model determine color text should be.

but cellforrowatindexpath should not trying adjust appearance of cell. have no assurances of order these instantiated (and may vary depending upon whether scrolling, direction, etc.).

if 1 did want update cell visible, dequeuereusablecellwithidentifier not correct method, regardless. instead, 1 use [tableview cellforrowatindexpath:] retrieves cell visible cell (and must not confused named uitableviewdatasource method). never in context because don't know if other cell had been loaded or not. (i think it's bad practice in general update cell in context, violation of separation of responsibilities.)


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -