IOS 模拟QQ分组折叠
#define USER_KEY @"users"
#define GROUPNAME_KEY @"groupName"
@interfaceViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *_tableView;
NSMutableArray *_dataSource;
BOOL expanded;
}
- (void)initializeDataSource;
- (void)initializeUserInterface;
- (void)buttonPressed:(UIButton *)sender;
- (BOOL)isExpanded:(int)section;
- (void)cellapseOrExpand:(int)section;
@end
@implementation ViewController
- (void)dealloc
{
[_tableViewrelease];
[_dataSourcerelease];
[super dealloc];
}
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
[selfinitializeDataSource];
[selfinitializeUserInterface];
}
- (void)initializeDataSource
{
_dataSource = [[NSMutableArrayarray] retain];
NSMutableDictionary *dict = [[NSMutableDictionaryalloc] init];
[dict setObject:@"Friend" forKey:GROUPNAME_KEY];
NSMutableArray *arr = [[NSMutableArrayalloc] init];
[arr addObject: @"Amanda"];
[arr addObject: @"Cecil"];
[arr addObject: @"Martin"];
[dict setObject:arr forKey:USER_KEY];
[arr release];
[_dataSource addObject:dict];
[dict release];
NSMutableDictionary *diction = [[NSMutableDictionaryalloc]init];
[diction setObject:@"WorkmMate" forKey:GROUPNAME_KEY];
NSMutableArray *array = [[NSMutableArrayalloc] init];
[array addObject: @"Eartha"];
[array addObject: @"Deirdre"];
[array addObject: @"Montague"];
[array addObject:@"Ken"];
[diction setObject:array forKey:USER_KEY];
[array release];
[_dataSource addObject:diction];
[diction release];
}
- (void)initializeUserInterface
{
self.view.backgroundColor = [UIColorwhiteColor];
_tableView = [[UITableViewalloc]initWithFrame:self.view.boundsstyle:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.viewaddSubview:_tableView];
[_tableViewrelease];
[_tableViewreloadData];
}
- (void)cellapseOrExpand:(int)section
{
expanded = NO;
NSMutableDictionary *data = [_dataSourceobjectAtIndex:section];
if ([data objectForKey:@"expanded"] != nil)
{
expanded = [[data objectForKey:@"expanded" ] boolValue];
}
[data setObject:[NSNumbernumberWithBool:!expanded] forKey:@"expanded"];
}
- (BOOL)isExpanded:(int)section
{
expanded = NO;
NSMutableDictionary *data = [_dataSourceobjectAtIndex:section];
if ([data objectForKey:@"expanded"] != nil)
{
expanded = [[data objectForKey:@"expanded" ] intValue];
}
returnexpanded;
}
#pragma make - <UITableViewDelegate>
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_dataSourcecount];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//当前为折叠
if (![self isExpanded:section])
{
return 0;
}
// 返回当前节点的数据
NSDictionary *data = [_dataSource objectAtIndex:section];
return [[data objectForKey:USER_KEY]count];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *heighView = [[UIViewalloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
heighView.backgroundColor = [UIColorcolorWithPatternImage:[UIImageimageNamed:@"line.png" ]];
UILabel *label = [[UILabelalloc]init];
label.bounds = CGRectMake(0, 0, 150, 30);
label.center = CGPointMake(120, 15);
NSDictionary *data = [_dataSource objectAtIndex:section];
label.text = [data objectForKey:GROUPNAME_KEY];
label.textAlignment = NSTextAlignmentLeft;
label.backgroundColor = [UIColorclearColor];
[heighView addSubview:label];
UIButton *button = [[UIButtonalloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
[button addTarget:selfaction:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
//保存结号,传递方法
button.tag = section;
//是否展开,切换按钮显示图片
if ([self isExpanded:section])
{
[button setImage:[UIImageimageNamed:@"btn_down"] forState:UIControlStateNormal];
}
else
{
[button setImage:[UIImageimageNamed:@"btn_right"] forState:UIControlStateNormal];
}
[heighView addSubview:button];
[button release];
return heighView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIndentifer = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifer];
if (!cell)
{
cell = [[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier: cellIndentifer]autorelease];
}
NSDictionary *data = (NSDictionary *)[_dataSourceobjectAtIndex:indexPath.section];
NSArray *personName = (NSArray *)[data objectForKey:USER_KEY];
if (personName == nil)
{
return cell;
}
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.textLabel.text = [personName objectAtIndex:indexPath.row];
return cell;
}
- (void)buttonPressed:(UIButton *)sender
{
[self cellapseOrExpand:sender.tag];
[_tableViewreloadData];
}
@end
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。