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

  • 相关阅读:
    iOS中3种正则表达式的使用
    iOS 正则表达式
    Autolayout-VFL语言添加约束-备
    PHP一个最简单的CMS内容管理系统
    国外主流PHP框架比较
    PHP中的ob_start() 的使用
    jpGraph的应用及基本安装配置 BY 命运
    未能加载文件或程序集“Common”或它的某一个依赖项。试图加载格式不正确的程序
    Android手机 Fildder真机抓包
    axWindowsMediaPlayer1获取音频长度
  • 原文地址:https://www.cnblogs.com/thbbsky/p/4168745.html
Copyright © 2011-2022 走看看