Tableview分组
简单Demo:http://download.csdn.net/detail/u012881779/8604713
添加下拉刷新Demo:http://download.csdn.net/detail/u012881779/8604963
每一组的第0行作为一级菜单,其它行作为二级菜单。我们要作的只是判断当前行是否被选中,并动态的更新当前组(section)的行数(row).
#import "GroupViewController.h"
#import "GroupTableViewCell.h"
@interface GroupViewController ()<UITableViewDataSource , UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *prepareArr;
@property (nonatomic) NSInteger groupMark;
@end
@implementation GroupViewController
@synthesize prepareArr = _prepareArr;
@synthesize groupMark = _groupMark;
-(void)dealloc{
_prepareArr = nil;
}
- (void)viewDidLoad {
[super viewDidLoad];
_groupMark = -1;
//数据准备
[self valueAction];
}
//数据准备
-(void)valueAction{
if(!_prepareArr){
_prepareArr = [[NSMutableArray alloc] init];
}
[_prepareArr removeAllObjects];
for (int i = 1; i < 15 ; i ++) {
NSMutableDictionary *valueDict = [[NSMutableDictionary alloc] init];
NSInteger valueId = arc4random()%1000;
[valueDict setObject:[NSString stringWithFormat:@"%d",valueId] forKey:@"id"];
[valueDict setObject:[NSString stringWithFormat:@"[%d]",valueId] forKey:@"name"];
[valueDict setObject:@"linkmanGroup.png" forKey:@"header"];
NSMutableArray *tempArr = [[NSMutableArray alloc] init];
for (int j = 1; j < 10 ; j ++) {
NSMutableDictionary *groupDict = [[NSMutableDictionary alloc] init];
NSInteger tempId = arc4random()%1000;
[groupDict setObject:[NSString stringWithFormat:@"%d",tempId+10000] forKey:@"id"];
[groupDict setObject:[NSString stringWithFormat:@"%d",tempId+10000] forKey:@"name"];
[groupDict setObject:@"linkmanIco.png" forKey:@"header"];
[tempArr addObject:groupDict];
}
[valueDict setObject:tempArr forKey:@"group"];
[_prepareArr addObject:valueDict];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
//几组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
return _prepareArr.count;
}
//几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSDictionary *tempDict = [_prepareArr objectAtIndex:section];
NSMutableArray *tempArr = [tempDict objectForKey:@"group"];
if(section == _groupMark){
if(_groupMark == -1){
}else{
return tempArr.count;
}
}else{
}
return 1;
}
//TableView赋值
- (UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier=@"GroupTableViewCell";
GroupTableViewCell *cell=[tableview dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = (GroupTableViewCell*)[[[NSBundle mainBundle] loadNibNamed:@"GroupTableViewCell" owner:nil options:nil] objectAtIndex:0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.backgroundColor=[UIColor clearColor];
[cell setClipsToBounds:YES];
NSInteger theRow = indexPath.row;
NSInteger theSection = indexPath.section;
if(theSection < _prepareArr.count){
NSDictionary *tempDict = [_prepareArr objectAtIndex:theSection];
if(theRow <= 0){
[cell valueAction:tempDict];
}else{
NSArray *tempArr = [tempDict objectForKey:@"group"];
if(theRow-1 < tempArr.count){
NSDictionary *groupDict = [tempArr objectAtIndex:theRow-1];
[cell valueAction:groupDict];
}else{
[cell cleanAction];
}
}
}else{
[cell cleanAction];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 44.0;
}
//选中时执行的操作
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.view endEditing:YES];
GroupTableViewCell *tempCell = (GroupTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
if(indexPath.row == 0){
NSLog(@"%d_%d",indexPath.row,indexPath.section);
if(_groupMark == indexPath.section){
_groupMark = -1;
}else{
_groupMark = -1;
[_tableView reloadData];
_groupMark = indexPath.section;
}
[_tableView reloadData];
}else{
NSDictionary *valueDict = tempCell.valueDict;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[valueDict objectForKey:@"name"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
}
}
@end
示图: