仿QQ好友 TableView 点击展开 收缩功能实现

在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;

}

转载于:https://www.cnblogs.com/rollrock/archive/2012/12/12/2814114.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值