uitableview - Checkmarks in tableview disappear when scrolling -


i building table different sections when scroll down checkmark disappears.

my table has around 10 sections divided arrays (one array each section).

also table not reuse cells (it set "nil").

// //  viewcontroller.m //  1234567 // //  created btb productionz on 8/17/14. //  copyright (c) 2014 unknown. rights reserved. //  #import "viewcontroller.h"  @interface viewcontroller () <uitableviewdatasource, uitableviewdelegate>     {     int info;     nsarray *section1;     nsarray *section2;     nsarray *section3;     nsarray *section4;     nsarray *section5;     nsarray *section6;     nsarray *section7;     nsarray *section8;     nsarray *section9;     nsarray *section10;   }  @end  @implementation viewcontroller  - (void)viewdidload {     [super viewdidload];        // additional setup after loading view, typically nib.     section1 = [nsarray arraywithobjects:@"item 1", @"item 2",@"item 3",@"item 4",@"item 5",@"item 6",@"item 7",@"item 8",@"item 9",@"item 10",@"item 11",@"item 12", nil];      section2 = [nsarray arraywithobjects:@"item 1", @"item 2",@"item 3",@"item 4",@"item 5",@"item 6",@"item 7",@"item 8",@"item 9", nil];       section3 = [nsarray arraywithobjects:@"item 1", @"item 2",@"item 3",@"item 4",@"item 5",@"item 6",@"item 7",@"item 8",@"item 9",@"item 10",@"item 11",@"item 12",@"item 13",@"item 14",@"item 15",@"item 16",@"item 17",@"item 18", nil];       section4 = [nsarray arraywithobjects:@"item 1", @"item 2",@"item 3",@"item 4",@"item 5",@"item 6",@"item 7",@"item 8",@"item 9",@"item 10", nil];      section5 = [nsarray arraywithobjects:@"item 1", @"item 2",@"item 3",@"item 4",@"item 5",@"item 6",@"item 7",@"item 8",@"item 9",@"item 10",@"item 11",@"item 12",@"item 13",@"item 14", nil];      section6 = [nsarray arraywithobjects:@"item 1", @"item 2",@"item 3",@"item 4",@"item 5",@"item 6",@"item 7",@"item 8",@"item 9",@"item 10",@"item 11",@"item 12",@"item 13", nil];      section7 = [nsarray arraywithobjects:@"item 1", @"item 2",@"item 3", nil];      section8 = [nsarray arraywithobjects:@"item 1", @"item 2",@"item 3",@"item 4",@"item 5",@"item 6",@"item 7",@"item 8", nil];      section9 = [nsarray arraywithobjects:@"item 1", @"item 2",@"item 3",@"item 4",@"item 5",@"item 6",@"item 7", nil];      section10 = [nsarray arraywithobjects:@"item 1", @"item 2",@"item 3",@"item 4",@"item 5",@"item 6",@"item 7",@"item 8",@"item 9",@"item 10", nil]; }  -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section{      if (section ==0) {         return [section1 count];}      else if (section ==1){return [section2 count];}     else if (section ==2){return [section3 count];}     else if (section ==3){return [section4 count];}     else if (section ==4){return [section5 count];}     else if (section ==5){return [section6 count];}     else if (section ==6){return [section7 count];}     else if (section ==7){return [section8 count];}     else if (section ==8){return [section9 count];}     else if (section ==9){return [section10 count];}       else{return 0;}  }  -(nsinteger)numberofsectionsintableview:(uitableview *)tableview{     return 10;  }    -(nsstring*)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section {     if (section ==0) {         return @"section 1";     }      else if (section ==1){return @"section 2";}     else if (section ==2){return @"section 3";}     else if (section ==3){return @"section 4";}     else if (section ==4){return @"section 5";}     else if (section ==5){return @"section 6";}     else if (section ==6){return @"section 7";}     else if (section ==7){return @"section 8";}     else if (section ==8){return @"section 9";}     else if (section ==9){return @"section 10";}       else {         return@" ";     }  }   - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {       static nsstring *simpletableidentifier = @"simpletablecell";      uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:simpletableidentifier];      if (cell == nil) {         cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:nil];     }     if (indexpath.section ==0) {         cell.textlabel.text = [section1 objectatindex:indexpath.row];     }      if (indexpath.section ==1) {         cell.textlabel.text = [section2 objectatindex:indexpath.row];     }       if (indexpath.section ==2) {         cell.textlabel.text = [section3 objectatindex:indexpath.row];     }      if (indexpath.section ==3) {         cell.textlabel.text = [section4 objectatindex:indexpath.row];     }      if (indexpath.section ==4) {         cell.textlabel.text = [section5 objectatindex:indexpath.row];     }      if (indexpath.section ==5) {         cell.textlabel.text = [section6 objectatindex:indexpath.row];     }      if (indexpath.section ==6) {         cell.textlabel.text = [section7 objectatindex:indexpath.row];     }      if (indexpath.section ==7) {         cell.textlabel.text = [section8 objectatindex:indexpath.row];     }      if (indexpath.section ==8) {         cell.textlabel.text = [section9 objectatindex:indexpath.row];     }      if (indexpath.section ==9) {         cell.textlabel.text = [section10 objectatindex:indexpath.row];     }     /*      cell.accessorytype = uitableviewcellaccessorynone;      */         return cell;   }     - (void)didreceivememorywarning {     [super didreceivememorywarning];     // dispose of resources can recreated. }     - (void)tableview:(uitableview *)thetableview didselectrowatindexpath:(nsindexpath *)newindexpath {      [thetableview deselectrowatindexpath:[thetableview indexpathforselectedrow] animated:no];     uitableviewcell *cell = [thetableview cellforrowatindexpath:newindexpath];     if (cell.accessorytype == uitableviewcellaccessorynone) {         cell.accessorytype = uitableviewcellaccessorycheckmark;         // reflect selection in data model     } else if (cell.accessorytype == uitableviewcellaccessorycheckmark) {         cell.accessorytype = uitableviewcellaccessorynone;         // reflect deselection in data model     } }       /*  - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath   {  uitableviewcell *cell = [tableview cellforrowatindexpath:indexpath];   if (cell.accessoryview == uitableviewcellaccessorynone) {  cell.accessorytype = uitableviewcellaccessorycheckmark;  }   else if (cell.accessorytype == uitableviewcellaccessorycheckmark){cell.accessorytype = uitableviewcellaccessorynone;}     }  */   @end 

see comment in code // reflect selection in data model

you need add logic in data model determine if cell should display checkmark or not.

and yes reusing cells unlike think, see line:

[tableview dequeuereusablecellwithidentifier:simpletableidentifier]; 

edit: should declare nsmutable array hold section arrays:

nsmutablearray *data; 

viewdidload

you need add property data model in order track if cells should selected or not. example :

section1 = [nsmutablearray arraywithobjects:@{@"title":@"item 1",@"isselected": @"n"},                                             @{@"title":@"item 2",@"isselected":@"n"},                                             @{@"title":@"item 3",@"isselected": @"n"},                                             @{@"title":@"item 4",@"isselected": @"n"}, nil]; 

after initializing section arrays, append them data source

data = [nsmutablearray arraywithobjects:section1, section2, section3,  section4, section5, section6, nil]; 

numberofrowsinsection

-(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section{     return [data[section] count]; } 

numberofsectionsintableview

-(nsinteger)numberofsectionsintableview:(uitableview *)tableview{     return [data count]; } 

cellforrowatindexpath

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {      static nsstring *simpletableidentifier = @"simpletablecell";      uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:simpletableidentifier];      if (cell == nil) {         cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:nil];     }     nsstring * title = data[indexpath.section][indexpath.row][@"title"];     bool isselected = [data[indexpath.section][indexpath.row][@"isselected"] isequaltostring:@"y"];      cell.textlabel.text = title;     if (!isselected) {         cell.accessorytype = uitableviewcellaccessorynone;     }else{         cell.accessorytype = uitableviewcellaccessorycheckmark;     }      return cell;   } 

didselectrowatindexpath

- (void)tableview:(uitableview *)thetableview didselectrowatindexpath:(nsindexpath *)newindexpath {      [thetableview deselectrowatindexpath:[thetableview indexpathforselectedrow] animated:no];     uitableviewcell *cell = [thetableview cellforrowatindexpath:newindexpath];     if (cell.accessorytype == uitableviewcellaccessorynone) {         cell.accessorytype = uitableviewcellaccessorycheckmark;         // reflect selection in data model         [data[newindexpath.section] replaceobjectatindex:newindexpath.row withobject:@{@"title":cell.textlabel.text,@"isselected": @"y"}];     } else if (cell.accessorytype == uitableviewcellaccessorycheckmark) {         cell.accessorytype = uitableviewcellaccessorynone;         // reflect deselection in data model          [data[newindexpath.section] replaceobjectatindex:newindexpath.row withobject:@{@"title":cell.textlabel.text,@"isselected": @"n"}];     } } 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -