码迷,mamicode.com
首页 > 移动开发 > 详细

iOS---》点击uitableview 的section展开或隐藏

时间:2014-12-17 10:38:49      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   io   color   os   sp   for   

#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

bubuko.com,布布扣

iOS---》点击uitableview 的section展开或隐藏

标签:style   blog   http   ar   io   color   os   sp   for   

原文地址:http://www.cnblogs.com/thbbsky/p/4168745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!