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