zoukankan      html  css  js  c++  java
  • 仿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;

    }

  • 相关阅读:
    DM8168通过GPMC接口与FPGA高速数据通信实现
    2016年 我在浙大计算机学院在职研究生学习经历
    CCS Font 知识整理总结
    Hexo 博客部署到 GitHub
    树莓派配置 USB 无线网卡
    树莓派搭建 Hexo 博客(二)
    树莓派搭建 Hexo 博客(一)
    树莓派初次使用的基本配置.md
    语法测试cnblogs使用Markdown
    硬件工程师-面试笔记0305
  • 原文地址:https://www.cnblogs.com/rollrock/p/2814114.html
Copyright © 2011-2022 走看看