objective c - I need to implement the expandable tableView cell in ios 8 -
in project need implement uitableview of tableview cells expandable , of them independent. if expandable cell need indicate '+' symbol.enter image description here. can 1 please me out
i have created small demo,
https://github.com/haripalwagh/expandabletableviewcell
screenshot 1 : before expanding cell
screenshot 2 : after expanding cell
@interface viewcontroller () <uitableviewdatasource, uitableviewdelegate> { uitableview *tblview; nsarray *cell0submenuitemsarray; bool issection0cell0expanded; } @end @implementation viewcontroller # pragma mark - view life cycle - (void)viewdidload { [super viewdidload]; tblview = [[uitableview alloc] initwithframe:cgrectzero style:uitableviewstylegrouped]; tblview.backgroundcolor = [uicolor clearcolor]; tblview.delegate = self; tblview.datasource = self; tblview.allowsselection = yes; tblview.scrollenabled = yes; tblview.alwaysbouncevertical = yes; [self.view addsubview:tblview]; cell0submenuitemsarray = @[@"first static menu item", @"second static menu item", @"third static menu item"]; } - (void)viewdidappear:(bool)animated { [super viewdidappear:animated]; [self.view setneedslayout]; } - (void)viewdidlayoutsubviews { [super viewdidlayoutsubviews]; [self updateviewdimensions]; } - (void)updateviewdimensions { tblview.frame = cgrectmake(0, 40, 320, 550); } - (void)didreceivememorywarning { [super didreceivememorywarning]; } # pragma mark - uitableview delegate , datasource - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { if (section == 0) { int cellcount = 2; // default count - if not single cell expanded if (issection0cell0expanded) { cellcount += [cell0submenuitemsarray count]; } return cellcount; } return 0; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *strcellid = @"cellid"; uitableviewcell *cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:strcellid]; cell.selectionstyle = uitableviewcellselectionstylenone; if (indexpath.section == 0) { if (indexpath.row == 0) { cell.textlabel.text = @"expandable cell"; uiimageview *accessoryimageview = [[uiimageview alloc] initwithframe:cgrectmake(0, 0, 30, 30)]; if (issection0cell0expanded) // set accessory view according cell state - expanded / not expanded { accessoryimageview.image = [uiimage imagenamed:@"minus.png"]; cell.detailtextlabel.text = @"status : expanded"; } else { accessoryimageview.image = [uiimage imagenamed:@"plus.png"]; cell.detailtextlabel.text = @"status : not expanded"; } cell.accessoryview = accessoryimageview; } else { if (issection0cell0expanded && [cell0submenuitemsarray count] >= indexpath.row) // check expanded status , necessary changes { cell.textlabel.text = [nsstring stringwithformat:@"%@", [cell0submenuitemsarray objectatindex:indexpath.row - 1]]; } else { cell.textlabel.text = @"static cell"; } } } return cell; } - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 1; } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { if (indexpath.row == 0) { // change status of cell reload table issection0cell0expanded = !issection0cell0expanded; [tblview reloaddata]; } }
you have manage every expandable cell. hope you..
Comments
Post a Comment