您的位置:首页 > 产品设计 > UI/UE

实现折叠的uitableviewcell效果

2016-01-09 21:30 465 查看
实现折叠的uitableviewcell效果思路:

效果就像QQ联系人列表那种,分几组,点击可以展开里面的子列表。一开始想到的可以使用tableview group的这种形式来做,今天介绍的不是使用group而是使用Plain来实现的,重点就在于实现section这块,我把section+cell看做一个整体的cell这样来处理,效果图如下:



整个cell是由一个View+tableview来组成的,上面的view可以当然head section ,下面的tableview可以当作是点击展开后的二级列表cell,基于这个只要处理点击后的高度计算就可以了。

项目里实现第一个head 和最后一个head是展开状态,其它只要点击时在展开的思路是:创建一个NSMutableArray来保存每个head
section的状态,在

heightForRowAtIndexPath方法里根据状态来计算高度。
在viewdidload里记录初始状态,
定义一个数组:

_sectionStatus = [[NSMutableArrayalloc]
init];
for (NSInteger i = 0; i <self.m_arr.count; i++) {
if (i==0) {
[_sectionStatus addObject:@1];
else if (i==(self.m_arr.count-1)){
[_sectionStatus addObject:@1];
}else{
[_sectionStatus addObject:@0];
}
}
[self.m_tableView reloadData];

heightForRowAtIndexPath方法里如下:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([_sectionStatus[indexPath.row] isEqualToNumber:@1]) {
ProblemBodyModel *cellData = self.m_arr[indexPath.row];

return cellData.m_Item2.count * 44 + 40;
}else{
return 40;
}
}


点击head section方法如下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//是否展开
BOOL status = [_sectionStatus[indexPath.row] boolValue];
NSNumber *num = status? @0 :@1;
[_sectionStatus replaceObjectAtIndex:indexPath.row withObject:num];
[self.m_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}


点击展开后的cell方法,在当前VC里实现自定义cell里tableview的点击方法的一个代理就可以了,如下:

#pragma mark -- 展开cell的点击方法
- (void)tableViewCellDidSelectWithUrlID:(NSString *)urlId andCellType:(NSString *)type;
{

}


到此就完成了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: