iOS 仿QQ二级列表

//DataModel.h

//二级列表

#import

@interfaceDataModel :NSObject

//联系数据

@property(strong,nonatomic)NSMutableArray* names;

//是否折叠

@property(assign,nonatomic,getter=isFolder)BOOLfolded;

//每组有多少分页

@property(assign,nonatomic)NSIntegersize;

//初始化联系人方法

- (instancetype)initWithNames:(NSMutableArray*)names;

@end


//DataModel.m

//二级列表

#import"DataModel.h"

@implementationDataModel

//初始化联系人方法

- (instancetype)initWithNames:(NSMutableArray*)names{

if(self= [superinit]) {

self.folded=YES;

_names= names;

}

returnself;

}

//每个组内有多少联系人

-(NSInteger)size{

return_names.count;

}



//ViewController.m

//二级列表

//

#import"ViewController.h"

#import"DataModel.h"

staticNSString* cellID =@"CELL_ID";

@interfaceViewController()

@property(strong,nonatomic)NSMutableArray* arrayList;

@property(strong,nonatomic)UITableView*myTabview;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

[selfloadData];

[selfsetupUI];

}

- (void)setupUI{

//创建tabView

_myTabview= [[UITableViewalloc]initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height)style:UITableViewStylePlain];

//遵守协议和数据源

_myTabview.delegate=self;

_myTabview.dataSource=self;

[_myTabviewregisterClass:[UITableViewCellclass]forCellReuseIdentifier:cellID];

//添加

[self.viewaddSubview:_myTabview];

}

//实现数据源方法

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{

return_arrayList.count;

}

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

DataModel* model =_arrayList[section];

returnmodel.folded?0:model.size;

//return 3;

}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

UITableViewCell* cell = [tableViewdequeueReusableCellWithIdentifier:cellIDforIndexPath:indexPath];

//将模型数据赋值给cell

DataModel* model =_arrayList[indexPath.section];

NSArray* array = model.names;

cell.textLabel.text= array[indexPath.row];

cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

returncell;

}

#warning显示组头视图必须设置组头视图的高

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{

return40;

}

//设置组头View

- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section{

//首先创建一个大的View

UIView* topView = [[UIViewalloc]init];

//设置边

topView.layer.borderWidth=0.2;

topView.layer.borderColor= [UIColorgrayColor].CGColor;

topView.backgroundColor= [UIColoryellowColor];

UILabel* label = [[UILabelalloc]initWithFrame:CGRectMake(40,0,self.view.bounds.size.width,40)];

NSArray* nameArray =@[@"朋友",@"家人",@"陌生人"];

label.text= nameArray[section];

[topViewaddSubview:label];

////创建Button按钮

UIButton* btn = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,40)];

btn.tag=200+ section;

[btnaddTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];

[topViewaddSubview:btn];

returntopView;

}

//请求数据

- (void)loadData{

//假设从后台请求下来的数据

NSArray* arrayData =@[@[@"大桥",@"小乔",@"周瑜"],@[@"刘备",@"关羽",@"张飞"],@[@"曹操",@"夏侯惇",@"许褚"]];

if(!_arrayList) {

_arrayList= [NSMutableArrayarray];

}

//数组转模型

for(NSMutableArray*arryMinarrayData) {

DataModel* model = [[DataModelalloc]initWithNames:arryM];

[_arrayListaddObject:model];

}

}

//折叠按钮响应方法

- (void)btnClick:(UIButton*)sender{

DataModel* groupModel =_arrayList[sender.tag-200];

groupModel.folded= !groupModel.isFolder;

[_myTabviewreloadData];

}

@end

留给自己 方便下次使用 不喜勿喷 谢谢

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容