ios - How to select all rows as default with checkmark? -
i want select rows when view appear, used index path in viewdidappear
selected 1 row , without checkmark here's viewdidappear
method:
-(void)viewdidappear:(bool)animated { [super viewdidappear:animated]; self.navigationcontroller.navigationbar.topitem.title = @"select blood donors"; nsindexpath *indexpath=[nsindexpath indexpathforrow:0 insection:0]; [_tblresult selectrowatindexpath:indexpath animated:yes scrollposition:uitableviewscrollpositionbottom]; }
my table view methods :
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { nsstring *cellident = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellident]; if(cell == nil) cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellident]; cell.textlabel.text = [arrname objectatindex:indexpath.row]; if([arselectedrows containsobject:indexpath]) { cell.accessorytype = uitableviewcellaccessorycheckmark; } else { cell.accessorytype = uitableviewcellaccessorynone; } return cell; } -(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *cell = [tableview cellforrowatindexpath:indexpath]; if(cell.accessorytype == uitableviewcellaccessorynone) { cell.accessorytype = uitableviewcellaccessorycheckmark; [arselectedrows addobject:indexpath]; } else { cell.accessorytype = uitableviewcellaccessorynone; [arselectedrows removeobject:indexpath]; } [tableview deselectrowatindexpath:indexpath animated:yes]; for(nsindexpath *indexpath in arselectedrows) { [selections addobject:[arrname objectatindex:indexpath.row]]; } } -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return [arrname count]; }
i got output according question requirement.i tried more 3 hour.if try boolean can't achieve have multiple checkbox.if have 1 checkbox can use boolean.
#import "viewcontroller.h" @interface viewcontroller () { nsmutablearray *arrproduct,*arrselectdeselectcheckmark; } @end @implementation secondviewcontroller @synthesize tblviewcheckmark; - (void)viewdidload { [super viewdidload]; // additional setup after loading view nib. arrproduct = [[nsmutablearray alloc]initwithobjects:@"ios",@"android",@"windows",@"blackberry",@"symbian",@"bada",nil]; arrselectdeselectcheckmark = [[nsmutablearray alloc]init]; for(int j=0;j<[arrproduct count];j++) { [arrselectdeselectcheckmark addobject:@"selected"]; } } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } #pragma mark - uitableviewdatasource methods -(nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 1; } -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return arrproduct.count; } -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { nsstring *strcell = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:strcell]; if(cell==nil) { cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:strcell]; } if([[arrselectdeselectcheckmark objectatindex:indexpath.row] isequaltostring:@"selected"]) cell.accessorytype = uitableviewcellaccessorycheckmark; else cell.accessorytype = uitableviewcellaccessorynone; cell.textlabel.text = [arrproduct objectatindex:indexpath.row]; return cell; } #pragma mark - uitableviewdelegate methods -(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *cell = [tableview cellforrowatindexpath:indexpath]; @try { cgpoint touchpoint = [cell convertpoint:cgpointzero toview:tblviewcheckmark]; nsindexpath *indexpath = [tblviewcheckmark indexpathforrowatpoint:touchpoint]; nslog(@"%@",arrselectdeselectcheckmark); if([arrselectdeselectcheckmark count]==0) { for(int i=0; i<[arrproduct count]; i++) { [arrselectdeselectcheckmark addobject:@"select"]; } } if([[arrselectdeselectcheckmark objectatindex:indexpath.row] isequaltostring:@"selected"]) { cell.accessorytype = uitableviewcellaccessorynone; [arrselectdeselectcheckmark replaceobjectatindex:indexpath.row withobject:@"deselected"]; } else { cell.accessorytype = uitableviewcellaccessorycheckmark; [arrselectdeselectcheckmark replaceobjectatindex:indexpath.row withobject:@"selected"]; } } @catch (nsexception *exception) { nslog(@"the exception is-%@",exception); } } @end
above code works perfectly.first select when view loads after can change that.
Comments
Post a Comment