Expansion contraction of IOS tableview cell
#import "ViewController.h"
@interface Viewcontroller () <uitableviewdatasource,uitableviewdelegate>{
Nsmutablearray *_allarray;//Creating an array of data sources
Nsmutabledictionary *dic;//Create a dictionary to decide whether to shrink or expand
}
@property (Nonatomic,strong) UITableView *tableview;
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Self.view.backgroundColor = [Uicolor Whitecolor];
DIC = [Nsmutabledictionary dictionary];
_allarray = [@[@[@ "12", @ "32", @ "12", @ "12", @ "12", @ "32"],@[@ "12", @ "12", @ "12", @ "32", @ "12", @ "12", @ "12", @ "32", @ "12", @ " "@", @ "a", @ "],@[@", @ "a", @ "+"]]mutablecopy];
[Self.view AddSubview:self.tableView];
}
Lazy Loading
-(UITableView *) tableview{
if (!_tableview) {
_tableview = [[Uitableviewalloc]initwithframe:cgrectmake (0, 375, 667-64) Style:uitableviewstyleplain];
_tableview.delegate = self;
_tableview.datasource = self;
}
return _tableview;
}
-(Nsinteger) Numberofsectionsintableview: (uitableview*) tableview{
return _allarray.count;
}
-(CGFloat) TableView: (UITableView *) TableView heightforheaderinsection: (Nsinteger) section{
return 30;
}
//
-(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (Nsinteger) section{
UIView *view = [UIView new];
View.backgroundcolor = [Uicolor Redcolor];
Create a gesture to click, this can be changed to a button
UITapGestureRecognizer *tap = [[Uitapgesturerecognizeralloc]initwithtarget:self Action: @selector (Action_tap:)];
View.tag = + Section;
[View Addgesturerecognizer:tap];
return view;
}
-(void) Action_tap: (Uigesturerecognizer *) tap{
NSString *str = [nsstringstringwithformat:@ "%ld", tap.view.tag-300];
if ([Dic[str] integervalue] = = 0) {//If it is 0, assign 1 to the dictionary and open the cell
[DiC setobject:@ "1" forkey:str];
}else{//Turn off cell instead
[dic setobject:@ "0" forkey:str];
}
[Self.tableview Reloaddata];
[Self.tableview reloadsections:[nsindexset indexsetwithindex:[str integervalue]]withrowanimation: uitableviewrowanimationfade];//Animated Refresh
}
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{
NSString *string = [nsstringstringwithformat:@ "%ld", section];
if ([dic[string] integervalue] = = 1) {//Open cell returns the count of the array
Nsarray *array = [nsarrayarraywitharray:_allarray[section]];
return array.count;
}else{
return 0;
}
}
-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{
Return 35;
}
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{
UITableViewCell *cell = [tableviewdequeuereusablecellwithidentifier:@ "cell"];
if (!cell) {
cell = [[uitableviewcellalloc]initwithstyle:uitableviewcellstylevalue1reuseidentifier:@ ' cell '];
}
Cell.backgroundcolor = [Uicolor Orangecolor];
Cell.textLabel.text = _allarray[indexpath.section][indexpath.row];
return cell;
}
@end
Expansion contraction of IOS tableview cell