仿QQ好友 TableView 点击展开 收缩功能实现
2012-12-12 10:14
741 查看
在IOS中,实现QQ好友一样的展开/收缩功能。使用的是UITableView控件
本文转自 http://www.999dh.net/article/iphone_ios_art/36.html 转载请注明,谢谢!
#import <UIKit/UIKit.h>
@interface CRViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
UITableView * tbView;
NSMutableArray * data;
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
data = [[NSMutableArray alloc] initWithCapacity:2];
NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:2];
[dict setObject:@"好朋友" forKey:@"groupname"];
NSMutableArray * arr = [[NSMutableArray alloc] initWithCapacity:2];
[arr addObject:@"1111"];
[arr addObject:@"2222"];
[arr addObject:@"3333"];
[arr addObject:@"4444"];
[dict setObject:arr forKey:@"users"];
[arr release];
[data addObject:dict];
[dict release];
dict = [[NSMutableDictionary alloc] initWithCapacity:2];
[dict setObject:@"对手" forKey:@"groupname"];
arr = [[NSMutableArray alloc] initWithCapacity:2];
[arr addObject:@"5555"];
[arr addObject:@"6666"];
[arr addObject:@"7777"];
[arr addObject:@"8888"];
[dict setObject:arr forKey:@"users"];
[arr release];
[data addObject:dict];
[dict release];
CGRect frame = [UIScreen mainScreen].applicationFrame;
frame.origin.y = 0;
tbView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
[tbView setDelegate:self];
[tbView setDataSource:self];
[self.view addSubview:tbView];
[tbView release];
[tbView reloadData];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return [data count];
}
-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellID = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if( nil == cell )
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
}
NSDictionary * m = (NSDictionary * )[data objectAtIndex:indexPath.section];
NSArray * d = (NSArray*)[m objectForKey:@"users"];
if( nil == d )
{
return cell;
}
cell.textLabel.text = [d objectAtIndex:indexPath.row];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"btn_listbg.png"]];
cell.imageView.image = [UIImage imageNamed:@"mod_user.png"];
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSDictionary * d = [data objectAtIndex:section];
//对指定节进行“展开”判断
if (![self isExpanded:section]) {
//若本节是“折叠”的,其行数返回为0
return 0;
}
return [[d objectForKey:@"users"] count];
}
-(void)collapseOrExpand:(int)section{
Boolean expanded = NO;
//Boolean searched = NO;
NSMutableDictionary* d=[data objectAtIndex:section];
//若本节model中的“expanded”属性不为空,则取出来
if([d objectForKey:@"expanded"]!=nil)
expanded=[[d objectForKey:@"expanded"]intValue];
//若原来是折叠的则展开,若原来是展开的则折叠
[d setObject:[NSNumber numberWithBool:!expanded] forKey:@"expanded"];
}
//返回指定节的“expanded”值
-(Boolean)isExpanded:(int)section{
Boolean expanded = NO;
NSMutableDictionary* d=[data objectAtIndex:section];
//若本节model中的“expanded”属性不为空,则取出来
if([d objectForKey:@"expanded"]!=nil)
expanded=[[d objectForKey:@"expanded"]intValue];
return expanded;
}
//按钮被点击时触发
-(void)expandButtonClicked:(id)sender{
UIButton* btn= (UIButton*)sender;
int section= btn.tag; //取得tag知道点击对应哪个块
// NSLog(@"click %d", section);
[self collapseOrExpand:section];
//刷新tableview
[tbView reloadData];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView * hView;
if( UIInterfaceOrientationLandscapeRight == [[UIDevice currentDevice] orientation] ||
UIInterfaceOrientationLandscapeLeft == [[UIDevice currentDevice] orientation])
{
hView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 480, 40)];
}
else
{
hView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
}
UIButton * eButton = [[UIButton alloc] init];
eButton.frame = hView.frame;
[eButton addTarget:self action:@selector(expandButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
eButton.tag = section;
if( [self isExpanded:section])
{
[eButton setImage:[UIImage imageNamed:@"btn_down.png"] forState:UIControlStateNormal];
}
else
{
[eButton setImage:[UIImage imageNamed:@"btn_right.png"] forState:UIControlStateNormal];
}
eButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[eButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 0, 0)];
[eButton setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 0, 0)];
eButton.backgroundColor = [UIColor lightGrayColor];
[eButton setTitle:[[data objectAtIndex:section] objectForKey:@"groupname"] forState:UIControlStateNormal];
[eButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[eButton setBackgroundImage:[UIImage imageNamed:@"btn_listbg.png"] forState:UIControlStateNormal];
[hView addSubview:eButton];
[eButton release];
return hView;
}
本文转自 http://www.999dh.net/article/iphone_ios_art/36.html 转载请注明,谢谢!
#import <UIKit/UIKit.h>
@interface CRViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
UITableView * tbView;
NSMutableArray * data;
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
data = [[NSMutableArray alloc] initWithCapacity:2];
NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:2];
[dict setObject:@"好朋友" forKey:@"groupname"];
NSMutableArray * arr = [[NSMutableArray alloc] initWithCapacity:2];
[arr addObject:@"1111"];
[arr addObject:@"2222"];
[arr addObject:@"3333"];
[arr addObject:@"4444"];
[dict setObject:arr forKey:@"users"];
[arr release];
[data addObject:dict];
[dict release];
dict = [[NSMutableDictionary alloc] initWithCapacity:2];
[dict setObject:@"对手" forKey:@"groupname"];
arr = [[NSMutableArray alloc] initWithCapacity:2];
[arr addObject:@"5555"];
[arr addObject:@"6666"];
[arr addObject:@"7777"];
[arr addObject:@"8888"];
[dict setObject:arr forKey:@"users"];
[arr release];
[data addObject:dict];
[dict release];
CGRect frame = [UIScreen mainScreen].applicationFrame;
frame.origin.y = 0;
tbView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
[tbView setDelegate:self];
[tbView setDataSource:self];
[self.view addSubview:tbView];
[tbView release];
[tbView reloadData];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return [data count];
}
-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellID = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if( nil == cell )
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
}
NSDictionary * m = (NSDictionary * )[data objectAtIndex:indexPath.section];
NSArray * d = (NSArray*)[m objectForKey:@"users"];
if( nil == d )
{
return cell;
}
cell.textLabel.text = [d objectAtIndex:indexPath.row];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"btn_listbg.png"]];
cell.imageView.image = [UIImage imageNamed:@"mod_user.png"];
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSDictionary * d = [data objectAtIndex:section];
//对指定节进行“展开”判断
if (![self isExpanded:section]) {
//若本节是“折叠”的,其行数返回为0
return 0;
}
return [[d objectForKey:@"users"] count];
}
-(void)collapseOrExpand:(int)section{
Boolean expanded = NO;
//Boolean searched = NO;
NSMutableDictionary* d=[data objectAtIndex:section];
//若本节model中的“expanded”属性不为空,则取出来
if([d objectForKey:@"expanded"]!=nil)
expanded=[[d objectForKey:@"expanded"]intValue];
//若原来是折叠的则展开,若原来是展开的则折叠
[d setObject:[NSNumber numberWithBool:!expanded] forKey:@"expanded"];
}
//返回指定节的“expanded”值
-(Boolean)isExpanded:(int)section{
Boolean expanded = NO;
NSMutableDictionary* d=[data objectAtIndex:section];
//若本节model中的“expanded”属性不为空,则取出来
if([d objectForKey:@"expanded"]!=nil)
expanded=[[d objectForKey:@"expanded"]intValue];
return expanded;
}
//按钮被点击时触发
-(void)expandButtonClicked:(id)sender{
UIButton* btn= (UIButton*)sender;
int section= btn.tag; //取得tag知道点击对应哪个块
// NSLog(@"click %d", section);
[self collapseOrExpand:section];
//刷新tableview
[tbView reloadData];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView * hView;
if( UIInterfaceOrientationLandscapeRight == [[UIDevice currentDevice] orientation] ||
UIInterfaceOrientationLandscapeLeft == [[UIDevice currentDevice] orientation])
{
hView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 480, 40)];
}
else
{
hView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
}
UIButton * eButton = [[UIButton alloc] init];
eButton.frame = hView.frame;
[eButton addTarget:self action:@selector(expandButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
eButton.tag = section;
if( [self isExpanded:section])
{
[eButton setImage:[UIImage imageNamed:@"btn_down.png"] forState:UIControlStateNormal];
}
else
{
[eButton setImage:[UIImage imageNamed:@"btn_right.png"] forState:UIControlStateNormal];
}
eButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[eButton setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 0, 0)];
[eButton setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 0, 0)];
eButton.backgroundColor = [UIColor lightGrayColor];
[eButton setTitle:[[data objectAtIndex:section] objectForKey:@"groupname"] forState:UIControlStateNormal];
[eButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[eButton setBackgroundImage:[UIImage imageNamed:@"btn_listbg.png"] forState:UIControlStateNormal];
[hView addSubview:eButton];
[eButton release];
return hView;
}
相关文章推荐
- iOS实现类似QQ的好友列表,自由展开折叠(在原来TableView的基础上添加一个字典,一个Button)
- 实现类似QQ聊天列表的segment点击切换tableView的数据功能
- android Textview 实现展开收缩功能+部分文字点击 (SpannableString)
- TextView的展开收缩功能的实现;(及判断TextView是否超出)
- UITableView仿QQ好友的分组收缩展开界面
- android Textview 实现展开收缩功能(字符宽度)(StaticLayout)
- iOS开发之tableView cell的展开收回功能实现代码
- JavaScript实现QQ列表展开收缩扩展功能
- UITableView实现类似QQ好友的折叠功能
- JS仿QQ好友列表展开、收缩功能(第二篇)
- UITableView仿QQ好友的分组收缩展开界面
- 源码推荐:长按拖拽排序,TableviewGroup阴影加圆角,图片浏览器,tableView展开收缩列表的功能,下拉放大,上推渐变隐藏
- IOS详解TableView —— QQ好友列表的实现
- ExtJs中实现tree节点,全部是单击展开和收缩效果,和收藏夹点击功能一样
- 用UICollectionView实现点击展开收缩的cell
- JS仿QQ好友列表展开、收缩功能(第一篇)
- IOS_实现tableview向上滚动全屏效果与点击展开动画
- Android仿QQ好友列表实现列表收缩与展开
- 点击UITableView的cell展开收缩
- 点击UITableView的cell展开收缩