点击tableview的头视图按钮来达到收放tableview的列表

//

//  ViewController.m

//  XIBForiPad

//

//  Created by Lu_Ca on 15/8/12.

//  Copyright (c) 2015 Lu_Ca. All rights reserved.

//

//

//点击tableview的头视图按钮来达到收放tableview的列表

//

#import "ViewController.h"

#import "XibForCell.h"

#import "XibModel.h"


@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>


@end


@implementation ViewController

{

    UITableView *_tabelView;

    NSMutableArray *_dataSource;

    UIButton *button;

    XibModel *_modle;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    _dataSource = [NSMutableArray array];

    _modle = [[XibModel alloc] init];

    _modle.isOn = YES;

    _tabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768) style:UITableViewStyleGrouped];

    _tabelView.delegate = self;

    _tabelView.dataSource = self;

    [self.view addSubview:_tabelView];

    

    for(NSInteger i = 0 ;i<15;i++){

        XibModel *model = [[XibModel alloc] init];

        model.name = [NSString stringWithFormat:@"小明%d",i];

        model.age = [NSString stringWithFormat:@"%d",10+i];

        model.sex = @"";

        [_dataSource addObject:model];

    }

    

    // Do any additional setup after loading the view, typically from a nib.

}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

    return 80;

}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

    button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(0, 0, 1024, 80);

    [button addTarget:self action:@selector(clickHeaderButton:) forControlEvents:UIControlEventTouchUpInside];

    button.backgroundColor = [UIColor redColor];

    return button;

}



//点击头视图按钮,是列表达到收放

- (void)clickHeaderButton:(UIButton *)sender

{

    //sender.selected = !sender.selected; 用这种方式来区分判断是不行的,因为每次刷新就会调用tableview的头视图,就会重新创建头视图的按钮。达不到效果,所以需要有一个页面加载后只创建一次的变量来记录

    _modle.isOn = !_modle.isOn;

    

    [_tabelView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];


    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];

    if(_modle.isOn){//选中

        [_tabelView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];

    }

    

}

//不同的情况不同的行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    if(_modle.isOn){

        return _dataSource.count;

    }

    return 0;

}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 122;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSString *cellid = @"xib";

    XibForCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];

    if(!cell){

        cell = [[[NSBundle mainBundle] loadNibNamed:@"XibForPad" owner:self options:nil] lastObject];

    }

    XibModel *model = _dataSource[indexPath.row];

    cell.name.text = model.name;

    cell.age.text = model.age;

    cell.sex.text = model.sex;

    return cell;

    

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值