zoukankan      html  css  js  c++  java
  • 类似于qq联系人的tablview能够展开和收缩

    在.h文件中定义三个数组和一个tablview

        UITableView *listTable;
        NSMutableArray *listArray;
        NSMutableArray *proviceArray;
        NSMutableArray *statusArray;

    //定义一个点击方法

    -(void)ClickTheSection:(int)section;

    在.m文件中使用

    //先声明数组和tablview并给数组赋初值

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        listTable=[[[UITableView alloc]initWithFrame:CGRectMake(0, 70, 320, 390) style:UITableViewStylePlain]autorelease];
        listTable.showsVerticalScrollIndicator=NO;
        listTable.backgroundColor=[UIColor whiteColor];
        listTable.dataSource=self;
        listTable.delegate=self;
        [self.view addSubview:listTable];

       listArray=[[NSMutableArray alloc]initWithObjects:@"河南",@"郑州",@"安阳",@"许昌",@"周口",@"南阳",@"信阳", nil];
        proviceArray=[[NSMutableArray alloc]initWithObjects:@"河南",@"河北",@"湖南",@"湖北",@"广东",@"广西",@"山西", nil];
        statusArray=[[NSMutableArray alloc]initWithObjects:@"0",@"0",@"0",@"0",@"0",@"0",@"0", nil];
        
    }

    #pragma mark-tablview的相关设置
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if ([[statusArray objectAtIndex:section]integerValue]) {
            return listArray.count;
        }else{
            return 0;
        }
        
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 60;
    }
    #pragma tablview的cell赋值方法
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *identfer=@"UItableviewCell";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identfer];
        if (cell==nil) {
            cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identfer]autorelease];
            cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle=UITableViewCellSelectionStyleGray;
            
            
            
        }    
        cell.textLabel.text=[listArray objectAtIndex:indexPath.row];
        return cell;
        
        
    }
    #pragma mark-tablview的section设置
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        
        UIImageView *imagView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 100)];
        imagView.backgroundColor=[UIColor clearColor];
        imagView.userInteractionEnabled=YES;
        [imagView setImage:[UIImage imageNamed:@"sectionbg.png"]];
        
        UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:[UIImage imageNamed:@"buttonbg.png"] forState:UIControlStateNormal];
        button.frame=CGRectMake(0, 10, 320, 80);
        [button setTitle:[proviceArray objectAtIndex:section] forState:UIControlStateNormal];
        button.tag=section;
        [button addTarget:self action:@selector(ClickTheSection:) forControlEvents:UIControlEventTouchUpInside];
        [imagView addSubview:button];
        
        return imagView;
            
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 100;
    }
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return proviceArray.count;
        
    }

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        if (section==0) {
            return @"123";
        }
        else if (section==1)
        {
            return @"456";
        }
        else{
            return @"789";
        }
    }

    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
        NSArray *array=[NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z", nil];
        return array;
    }
    #pragma mark-移动问题

    -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
    {
        [listArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
        [listTable  reloadData];
        
    }
    #pragma mark-点击某个section的效应
    -(void)ClickTheSection:(int)section
    {
        UIButton *button=(UIButton*)section;
        if ([[statusArray objectAtIndex:button.tag]integerValue]==0) {
            [statusArray replaceObjectAtIndex:button.tag withObject:@"1"];
        }
        else{
            [statusArray replaceObjectAtIndex:button.tag withObject:@"0"];
        }
        [listTable reloadData];
        
    }

    #pragma mark-点击按钮触发事件

    -(void)ClickEditButton
    {
        if (listTable.editing==YES) {
            [listTable setEditing:NO animated:YES];
        }
        else{
            [listTable setEditing:YES animated:YES];
        }

        
    }


  • 相关阅读:
    第五章.函数
    第四章.文件操作
    第三章.数据类型
    PyYaml简单学习
    Vim编辑器基本用法
    numpy.ndarray.transpose用法理解
    Django Formsets总结
    学习,认知,思维
    Django model总结(上)
    结合pandas,sqlite3批量将csv数据导入sqlite数据库
  • 原文地址:https://www.cnblogs.com/riskyer/p/3299291.html
Copyright © 2011-2022 走看看