• 类似于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];
        }

        
    }


  • 相关阅读:
    uni-app之预加载和取消预加载(仅支持APP和H5)——uni.preloadPage、uni.unPreloadPage
    JavaScript 之数组对象(Array)
    【2019csp模拟】文件列表
    【2019csp模拟】两段子序列
    B. 【普转提七联测 Day 6】载重
    C.【普转提七联测 Day 6】分数
    A. 【普转提七联测 Day 6】石头
    struct和class的区别
    TagHelper中获取当前Url
    为什么要使用 Taghelper (标记助手)
  • 原文地址:https://www.cnblogs.com/riskyer/p/3299291.html
Copyright © 2020-2023  润新知