公司里要做个一个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;
}
网友评论