#import "yxpGroupTBVC.h"
#define DIC_EXPANDED @"expanded" //是否是展开 0收缩 1展开
#define DIC_ARARRY @"array"
#define DIC_TITILESTRING @"title"
#define CELL_HEIGHT 40.0f
@interfaceyxpGroupTBVC ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *_tableVIew;
NSMutableArray *_DataArray;
}
@end
@implementation yxpGroupTBVC
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
/*
主要思路:
1.tableView:tableView viewForHeaderInSection:section 添加一个按钮
2.点击按钮后,判断指定section的数据是否展开
3.在返回numberOfRowsInSection数量时,如果发现是收缩的,则返回0,展开时,才给真实数据的行号
这样就可以达到显示/隐含数据的效果
*/
}
returnself;
}
//初始化数据
- (void)initDataSource
{
//创建一个数组
_DataArray=[[NSMutableArrayalloc] init];
for (int i=0;i<=5 ; i++) {
NSMutableArray *array=[[NSMutableArrayalloc] init];
for (int j=0; j<=5;j++) {
NSString *string=[NSStringstringWithFormat:@"%i组-%i行",i,j];
[arrayaddObject:string];
}
NSString *string=[NSStringstringWithFormat:@"第%i分组",i];
//创建一个字典 包含数组,分组名,是否展开的标示
NSMutableDictionary *dic=[[NSMutableDictionaryalloc] initWithObjectsAndKeys:array,DIC_ARARRY,string,DIC_TITILESTRING,[NSNumbernumberWithInt:0],DIC_EXPANDED,nil];
//将字典加入数组
[_DataArrayaddObject:dic];
}
}
//初始化表
- (void)initTableView
{
_tableVIew=[[UITableViewalloc] initWithFrame:self.view.boundsstyle:UITableViewStylePlain];
_tableVIew.dataSource=self;
_tableVIew.delegate=self;
[self.viewaddSubview:_tableVIew];
}
- (void)viewDidLoad
{
[superviewDidLoad];
[selfinitDataSource];
[selfinitTableView];
}
#pragma mark -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return_DataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSMutableDictionary *dic=[_DataArrayobjectAtIndex:section];
NSArray *array=[dic objectForKey:DIC_ARARRY];
//判断是收缩还是展开
if ([[dicobjectForKey:DIC_EXPANDED]intValue]) {
return array.count;
}else
{
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *acell=@"cell";
UITableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier:acell];
if (!cell) {
cell=[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:acell];
}
NSArray *array=[[_DataArrayobjectAtIndex:indexPath.section] objectForKey:DIC_ARARRY];
cell.textLabel.text=[arrayobjectAtIndex:indexPath.row];
return cell;
}
//设置分组头的视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *hView = [[UIViewalloc]initWithFrame:CGRectMake(0,0, 320, CELL_HEIGHT)];
hView.backgroundColor=[UIColorwhiteColor];
UIButton* eButton = [[UIButtonalloc] init];
//按钮填充整个视图
eButton.frame = hView.frame;
[eButtonaddTarget:selfaction:@selector(expandButtonClicked:)
forControlEvents:UIControlEventTouchUpInside];
//把节号保存到按钮tag,以便传递到expandButtonClicked方法
eButton.tag = section;
//设置图标
//根据是否展开,切换按钮显示图片
if ([self isExpanded:section])
[eButton setImage: [ UIImageimageNamed: @"mark_up" ]forState:UIControlStateNormal];
else
[eButton setImage: [ UIImageimageNamed: @"mark_down" ]forState:UIControlStateNormal];
//设置分组标题
[eButton setTitle:[[_DataArrayobjectAtIndex:section] objectForKey:DIC_TITILESTRING]forState:UIControlStateNormal];
[eButton setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
//设置button的图片和标题的相对位置
//4个参数是到上边界,左边界,下边界,右边界的距离
eButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
[eButton setTitleEdgeInsets:UIEdgeInsetsMake(5,5, 0,0)];
[eButton setImageEdgeInsets:UIEdgeInsetsMake(5,300, 0,0)];
//上显示线
UILabel *label1=[[UILabelalloc] initWithFrame:CGRectMake(0, -1, hView.frame.size.width,1)];
label1.backgroundColor=[UIColorskyBlueColor];
[hViewaddSubview:label1];
//下显示线
UILabel *label=[[UILabelalloc] initWithFrame:CGRectMake(0, hView.frame.size.height-1, hView.frame.size.width,1)];
label.backgroundColor=[UIColorskyBlueColor];
[hViewaddSubview:label];
[hViewaddSubview: eButton];
return hView;
}
//单元行内容递进
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 2;
}
//控制表头分组表头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
returnCELL_HEIGHT;
}
#pragma mark -- 内部调用
//对指定的节进行“展开/折叠”操作,若原来是折叠的则展开,若原来是展开的则折叠
-(void)collapseOrExpand:(int)section{
NSMutableDictionary *dic=[_DataArrayobjectAtIndex:section];
int expanded=[[dic objectForKey:DIC_EXPANDED] intValue];
if (expanded) {
[dic setValue:[NSNumbernumberWithInt:0]forKey:DIC_EXPANDED];
}else
{
[dic setValue:[NSNumbernumberWithInt:1]forKey:DIC_EXPANDED];
}
}
//返回指定节是否是展开的
-(int)isExpanded:(int)section{
NSDictionary *dic=[_DataArrayobjectAtIndex:section];
int expanded=[[dic objectForKey:DIC_EXPANDED] intValue];
return expanded;
}
//按钮被点击时触发
-(void)expandButtonClicked:(id)sender{
UIButton* btn= (UIButton*)sender;
int section= btn.tag;//取得tag知道点击对应哪个块
[selfcollapseOrExpand:section];
//刷新tableview
[_tableVIewreloadData];
}
@end
IOS总结_可收缩分组表格(仿QQ联系人界面),布布扣,bubuko.com
原文地址:http://blog.csdn.net/yxiaoping/article/details/38707417