#import "GCZRootViewController.h"
@interface GCZRootViewController ()
@property (strong,nonatomic) NSMutableArray* dataSource;
// 用来标记每一个分区的状态,展开状态就记录YES,否则记录为NO
@property (strong,nonatomic) NSMutableArray* flags;
@end
@implementation GCZRootViewController
{
UITableView* _tableView;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[superviewDidLoad];
[selfcreateUI];
[selfcreateDatasource];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark- UI
- (void)createUI
{
_tableView = [[UITableViewalloc] initWithFrame:CGRectMake(10,30, 300, 440)style:UITableViewStylePlain];
_tableView.delegate =self;
_tableView.dataSource =self;
[self.viewaddSubview:_tableView];
}
- (void)createDatasource
{
_dataSource = [[NSMutableArrayalloc] init];
for (int i='A'; i<='Z'; i++) {
NSMutableArray* section = [[NSMutableArrayalloc] init];
for (int j=0; j<5; j++) {
NSString* str = [NSStringstringWithFormat:@"%c-%d",i,j];
[sectionaddObject:str];
}
[_dataSourceaddObject:section];
}
//标记每一个分区默认是展开状态
_flags = [[NSMutableArrayalloc] init];
for (int i=0; i<_dataSource.count; i++) {
NSNumber* flagNum = [NSNumbernumberWithBool:YES];
[_flagsaddObject:flagNum];
}
}
#pragma mark- UITableViewDatasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//当用户点击某个分组的标题按钮时,如果这个分区需要做隐藏,那就返回0,如果不需隐藏行操作,就返回原来的数据
//那么判断条件该如何确定呢?
// NSNumber* flagNum = [_flags objectAtIndex:section];
// BOOL flag = [flagNum boolValue];
//
// if (flag == NO) {
// return 0;
// }else
return [[self.dataSourceobjectAtIndex:section] count];
}
// 使用另一种方式处理隐藏,把行高更新成0
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSNumber* flagNum = [_flagsobjectAtIndex:indexPath.section];
BOOL flag = [flagNum boolValue];
if (flag == NO) {
return 0.1;
}
return 44;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* identifier =@"gczcell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];
}
cell.textLabel.text = [[self.dataSourceobjectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
return cell;
}
// 创建标题视图为一个按钮,这样就可以接受用户事件了
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton* btn = [UIButtonbuttonWithType:UIButtonTypeSystem];
[btn setTintColor:[UIColorblackColor]];
NSNumber* oldFlag = [_flagsobjectAtIndex:section];
BOOL oflag = [oldFlag boolValue];
if (oflag) {
[btn setTitle:@"关闭"forState:UIControlStateNormal];
}else
[btn setTitle:@"展开"forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColororangeColor]];
btn.tag = section+1;
[btn addTarget:selfaction:@selector(titleViewClicked:)forControlEvents:UIControlEventTouchUpInside];
return btn;
}
// 返回分区标题的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
- (void)titleViewClicked:(UIButton*)sender
{
// // 解决了回调方法的再一次调用
// [_tableView reloadData];
NSNumber* oldFlag = [_flagsobjectAtIndex:sender.tag-1];
BOOL oflag = [oldFlag boolValue];
//使用相反的状态值替换数组内原来的那个表示状态开关的值
NSNumber* newFlag = [NSNumbernumberWithBool:!oflag];
[_flagsreplaceObjectAtIndex:sender.tag-1withObject:newFlag];
//核心代码,(重新加载的那个区)
NSIndexSet* set = [[NSIndexSetalloc] initWithIndex:sender.tag-1];
[_tableViewreloadSections:set withRowAnimation:UITableViewRowAnimationNone];
}
@end
587

被折叠的 条评论
为什么被折叠?



