zoukankan      html  css  js  c++  java
  • iOS---》点击uitableview 的section展开或隐藏

    #import <UIKit/UIKit.h>
    
    @interface TestCell : UITableViewCell
    
    @property (weak, nonatomic) IBOutlet UILabel *firstLabel;
    
    @property (weak, nonatomic) IBOutlet UILabel *endLabel;
    @property (weak, nonatomic) IBOutlet UIView *myView;
    
    @end
    #import "TestCell.h"
    
    @implementation TestCell
    
    - (void)awakeFromNib {
        _myView.layer.borderColor=[UIColor clearColor].CGColor;
        
        
        // Initialization code
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
        [super setSelected:selected animated:animated];
    
        // Configure the view for the selected state
    }
    
    @end
    
    
    #import "ViewController.h"
    #import "TestCell.h"
    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
    {
    
        NSMutableArray *rowArray;
        BOOL *flag;
    }
    @property (weak, nonatomic) IBOutlet UITableView *myTableView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
       
        rowArray=[NSMutableArray arrayWithObjects:@"1",@"2",@"3", @"4",@"5",@"6",@"7",@"8",@"9",nil];
        flag = (BOOL*)malloc(rowArray.count*sizeof(BOOL*));
         memset(flag, NO, sizeof(*flag));
        //_myTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return rowArray.count;
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (flag[section]) {
            return rowArray.count;
        }
        else
        {
            return 0;
        }
    
    }
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       
        TestCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TestCell"];
        cell.firstLabel.text=rowArray[indexPath.row];
        cell.endLabel.text=rowArray[indexPath.row];
        
        return cell;
    }
    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        if (section==0) {
            return [self firstView];
        }
        
       return  [self sectionView:section];
    }
    -(UIView *)firstView
    {
         UIView *contentView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 32)];
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 15, 40, 15)];
        label.text=@"--1--";
        [contentView addSubview:label];
        return contentView;
    }
    -(UIView *)sectionView:(NSInteger)section
    {
        UIView *contentView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 32)];
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame=CGRectMake(375-50, 15, 30, 16);
        btn.tag=section;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(10, 15, 40, 15)];
        label.text=@"测试";
        if(flag[section])
        {
            [ btn setBackgroundImage:[UIImage imageNamed:@"open"] forState:UIControlStateNormal];
        }
        else
        {
            [ btn setBackgroundImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
        }
        [contentView addSubview:btn];
        [contentView addSubview:label];
        contentView.layer.borderColor=[UIColor lightGrayColor].CGColor;
        contentView.layer.borderWidth=1.0;
        contentView.alpha=1.0;
        return contentView;
    
    }
    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 40;
    }
    -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 2;
    }
    -(void)btnClick:(UIButton *)sender
    {
        int index=(int)sender.tag;
        flag[index]=!flag[index];
        [_myTableView reloadData];
    }
    
    @end

  • 相关阅读:
    用Margin还是用Padding?
    更优雅的清除浮动float方法
    清除浮动float (:after方法)
    px,em,rem
    load()方法
    PHP函数详解:call_user_func()使用方法
    移动端touch事件影响click事件以及在touchmove添加preventDefault导致页面无法滚动的解决方法
    Mysql开启远程连接方法
    mysql的字符串连接符
    php使用curl访问https返回无结果的问题
  • 原文地址:https://www.cnblogs.com/thbbsky/p/4168745.html
Copyright © 2011-2022 走看看