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

  • 相关阅读:
    HTML基础(一)基本语法知识
    本地方法接口
    本地方法栈
    虚拟机栈相关的问题
    栈帧的内部结构--一些附加信息
    基于角色的权限控制设计
    SpringBoot普通消息队列线程池配置
    栈帧的内部结构--动态返回地址(Return Address)
    栈帧的内部结构--动态链接 (Dynamic Linking)
    栈帧的内部结构--操作数栈(Opreand Stack)
  • 原文地址:https://www.cnblogs.com/thbbsky/p/4168745.html
Copyright © 2011-2022 走看看