公司里要做個一個Tableview的折疊效果.大概想了一下,有三種實現.1.直接建立Tableview,使用UIView的animateWithDuration動畫.動態改變cell的大小 2. 用SectionHeaderView 來做折疊下的cell.通過數組的方式控制tableCell的顯示. 3.是在網上看到的一個例子 用的是model來形成一個數據結構,自定義cell.
1. animateWithDuration
這種方法其實不是很好. 有時候會出現空指針.很多問題.不推薦.
2.SectionHeader
SectionHeader偽裝的Cell
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
[button setTag:section+1];
button.backgroundColor = [UIColor lightGrayColor];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 60)];
[button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
UILabel *tlabel = [[UILabel alloc]initWithFrame:CGRectMake(45, (kCell_Height-20)/2, 200, 20)];
[tlabel setBackgroundColor:[UIColor clearColor]];
[tlabel setFont:[UIFont systemFontOfSize:14]];
[tlabel setText:sectionArray[section]];
[button addSubview:tlabel];
return button;
}
邏輯判斷
//In the first
//Section開頭的名字
sectionArray = [NSMutableArray arrayWithObjects:@"使用步驟",
@"正常人體溫度",nil];
//狀態邏輯
stateArray = [NSMutableArray array];
for (int i = 0; i < 2; i++)
{
//所有的分區都是閉合
[stateArray addObject:@"0"];
}
//headButton點擊時,就修改狀態值.
- (void)buttonPress:(UIButton *)sender
{
//判斷狀態值
if ([stateArray[sender.tag - 1] isEqualToString:@"1"]){
//修改
[stateArray replaceObjectAtIndex:sender.tag - 1 withObject:@"0"];
}else{
[stateArray replaceObjectAtIndex:sender.tag - 1 withObject:@"1"];
}
//每次點擊后,會重載整個TableView
[helpView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag-1] withRowAnimation:UITableViewRowAnimationAutomatic];
}
//根據stateArray的狀態來決定是否顯示Row.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([stateArray[section] isEqualToString:@"1"]){
//如果是展開狀態
// NSArray *array = [dataSource objectAtIndex:section];
// return array.count;
return 1;
}else{
//如果是閉合,返回0
return 0;
}
}
在cellForRowAtIndexPath里設置內容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
//自定義cell
[tableView registerClass:[ThermometerHistoryCell class] forCellReuseIdentifier:@"cell"];
ThermometerHistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.titleLabel.text = @"使用步驟";
return cell;
}else if(indexPath.section == 1){
static NSString *identifier = @"bell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
UIImage *img =[UIImage imageNamed:@"biaoge"];
UIImageView *formView = [[UIImageView alloc]initWithFrame:CGRectMake(9, 60, ScreenWidth-18, img.size.height)];
formView.image = img;
[cell.contentView addSubview:formView];
return cell;
}
return nil;
}