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

来源:互联网 发布:全新英朗刷启停软件 编辑:程序博客网 时间:2024/03/29 00:15

//

//  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 {

    [superviewDidLoad];

    _dataSource = [NSMutableArrayarray];

   _modle = [[XibModelalloc] init];

    _modle.isOn =YES;

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

    _tabelView.delegate =self;

    _tabelView.dataSource =self;

    [self.viewaddSubview:_tabelView];

    

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

       XibModel *model = [[XibModelalloc] init];

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

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

        model.sex =@"";

        [_dataSourceaddObject: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 = [UIButtonbuttonWithType:UIButtonTypeCustom];

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

    [buttonaddTarget:selfaction:@selector(clickHeaderButton:)forControlEvents:UIControlEventTouchUpInside];

    button.backgroundColor = [UIColorredColor];

   return button;

}



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

- (void)clickHeaderButton:(UIButton *)sender

{

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

    _modle.isOn = !_modle.isOn;

    

    [_tabelViewreloadSections:[NSIndexSetindexSetWithIndex:0]withRowAnimation:UITableViewRowAnimationAutomatic];


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

    if(_modle.isOn){//选中

        [_tabelViewscrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddleanimated: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 = [[[NSBundlemainBundle] loadNibNamed:@"XibForPad"owner:selfoptions: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 {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


1 0
原创粉丝点击