QQ列表

246 阅读2分钟

方式1:

#import "ViewController.h"

@interface ViewController () {

intsectionCounts[5];

}

@property (nonatomic, weak) UITableView *tableView;

@property (nonatomic, strong) NSArray *dataArr;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.


UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

tableView.dataSource=self;

tableView.delegate=self;

tableView.estimatedSectionHeaderHeight = 0;

tableView.estimatedSectionFooterHeight = 0;

[self.viewaddSubview:tableView];

self.tableView= tableView;


//

self.dataArr = @[@[@"2222",@"11", @"3333", @"ww", @"qqq"], @[@"ww", @"eeeeeee"], @[@" 对的", @"得到的", @"钱钱钱"], @[@"试试"], @[@"让他有太阳", @"吞吞吐吐"]];

}

#pragma mark -- UITableViewDataSource

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

return 5;

}

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


if(sectionCounts[section]) {

return[self.dataArr[section]count];

}


return 0;

}

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

staticNSString*cellID =@"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

if(!cell) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];

}

NSArray*rowDataArr =self.dataArr[indexPath.section];

cell.textLabel.text= [NSStringstringWithFormat:@"----------%@",rowDataArr[indexPath.row]];

returncell;

}

#pragma mark -- UITableViewDelegate

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {

return 100;

}

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

return 50;

}

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

return 20;

}

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

UIButton*sectionBtn = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,375,100)];

[sectionBtnaddTarget:self action:@selector(sectionBtnClick:) forControlEvents:UIControlEventTouchUpInside];

sectionBtn.backgroundColor= [UIColorredColor];

sectionBtn.tag= section;

[sectionBtnsetTitle:[NSString stringWithFormat:@"------%ld", section] forState:UIControlStateNormal];

returnsectionBtn;

}

- (void)sectionBtnClick:(UIButton*)sectionBtn {

NSLog(@"---------sdfsdfs---------:%ld", sectionBtn.tag);


intindex = (int)sectionBtn.tag;

NSLog(@"---------ddddd---------:%d", sectionCounts[index]);

sectionCounts[index] = !sectionCounts[index];


[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationFade];

}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {

[tableViewdeselectRowAtIndexPath:indexPath animated:true];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end


方式2:

#import "ViewController.h"

#import "HZGroupModel.h"

@interface ViewController ()

@property (nonatomic, weak) UITableView *tableView;

@property (nonatomic, strong) NSArray *dataBgArr;

//** <#注释#>*/

@property (nonatomic, strong) NSMutableArray *modelArr;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.


UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];

tableView.dataSource=self;

tableView.delegate=self;

tableView.estimatedSectionHeaderHeight = 0;

tableView.estimatedSectionFooterHeight = 0;

[self.viewaddSubview:tableView];

self.tableView= tableView;

self.dataBgArr = @[@[@"aa", @"ss", @"ddd", @"ff", @"eee"], @[@"qq", @"gg", @"kk"], @[@"vv", @"ccc", @"hhh", @"kkk"], @[@"ccc", @"xxx"], @[@"vvv", @"uuu", @"fdffdfdfddf", @"hjhjhjhhjh"]];

self.modelArr = [NSMutableArray array];

for(NSArray*arrinself.dataBgArr) {


NSLog(@"----arr-----%@", arr);

HZGroupModel*groupModel = [HZGroupModelnew];

groupModel.isOpened=false;

groupModel.modelArr= arr;

[self.modelArraddObject:groupModel];

}


NSLog(@"----sss-----%@", self.modelArr);


[self.tableView reloadData];

}

#pragma mark -- UITableViewDataSource

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

return self.modelArr.count;

}

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


HZGroupModel*groupModel =self.modelArr[section];

if(groupModel.isOpened) {

returngroupModel.modelArr.count;

}

return 0;

}

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

staticNSString*cellID =@"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

if(!cell) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];

}

HZGroupModel*groupModel =self.modelArr[indexPath.section];

cell.textLabel.text= [NSStringstringWithFormat:@"----------%@",groupModel.modelArr[indexPath.row]];

returncell;

}

#pragma mark -- UITableViewDelegate

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {

return 100;

}

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

return 50;

}

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

return 20;

}

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


UIButton*sectionBtn = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,375,100)];

[sectionBtnaddTarget:self action:@selector(sectionBtnClick:) forControlEvents:UIControlEventTouchUpInside];

sectionBtn.backgroundColor= [UIColorredColor];

sectionBtn.tag= section +101;

[sectionBtnsetTitle:[NSString stringWithFormat:@"------%ld", section] forState:UIControlStateNormal];


returnsectionBtn;

}

- (void)sectionBtnClick:(UIButton*)sectionBtn {

NSIntegerindex = sectionBtn.tag-101;

//

NSLog(@"----self.modelArr-----%@", self.modelArr);

//

HZGroupModel*groupModel =self.modelArr[index];

NSLog(@"----groupModel-1----%d", groupModel.isOpened);

groupModel.isOpened= !groupModel.isOpened;

NSLog(@"----groupModel-2----%d", groupModel.isOpened);

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationFade];

}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {

[tableViewdeselectRowAtIndexPath:indexPath animated:true];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}


HZGroupModel的.h文件

#import

@interfaceHZGroupModel :NSObject

@property (nonatomic, strong) NSArray *modelArr;

@property (nonatomic, assign) BOOL isOpened;

@end