可摺疊tableView - 仿照通信錄

 個人xcode 是    IOS9.0    Xcode.7.0xcode

版本符合,可複製黏貼直接用ide

#import "ViewController.h"spa

 1.定義成員變量 遵循代理代理

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>code

{orm

    NSMutableArray *_switchArray;事件

    NSMutableArray *_contentArray;get

    UITableView *_tableView;string

}it

 

@end

@implementation ViewController

 

2.  viewDidLoad  裏

- (void)viewDidLoad {

    [super viewDidLoad];

    [self getData];

    [self addUITableView];

}

3. addUITableView 裏

 

- (void)addUITableView{

    _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain]; //設置爲樸素樣式

    _tableView.delegate = self;

    _tableView.dataSource = self;

    [self.view addSubview:_tableView];

}

//UITableViewDataSource 方法

/*

必須實現的方法

 方法一: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

 方法二:  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

可選實現的方法

  方法一: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

  方法二: - (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

  方法三:- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

  方法四:- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

  方法五:- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

  方法六:- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView

  方法七:- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index

  方法八:- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

  方法九:- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

*/

4 numberOfRowsInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (![[_switchArray objectAtIndex:section] boolValue]) {

        return 0;// 閉合狀態

    }else{

        return [[_contentArray objectAtIndex:section] count];// 每一組的行數

    }

}

5.cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSString *cellID = @"CELL";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

    }

    NSArray *array = _contentArray[indexPath.section];// 取出對應組

    NSString *title = array[indexPath.row];//取出對應組

    cell.textLabel.text = title;

    return cell;

}

6.titleForHeaderInSection 頭標題

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    return @"fwz";

}

7. viewForHeaderInSection :頭視圖

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIButton *button = [[UIButton alloc]init];

    [button setTitle:[NSString stringWithFormat:@"%c",(char)('A' + section)] forState:UIControlStateNormal];

    NSLog(@"1%@",button.titleLabel.text);

    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    button.tag = 100 + section;

    [button addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];

    return button;

}

 

8.tap button的點擊事件

-(void)tap:(UIButton *)sender{

    //1.取出BOOL值

    BOOL isOpen = [[_switchArray objectAtIndex:sender.tag - 100] boolValue];

    // 2.把Bool 取反

    [_switchArray replaceObjectAtIndex:sender.tag - 100 withObject:[NSNumber numberWithBool:!isOpen]];

    // 3.tableview作相應的操做(刷新某一組)

    [_tableView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag -100] withRowAnimation:UITableViewRowAnimationAutomatic];

    

    

}

 

9.getData 

- (void)getData{

    //記錄咱們組的開關裝態

    _switchArray = [NSMutableArray array];

    _contentArray = [NSMutableArray array];

    for (int i = 'A'; i <= 'Z'; i ++) {

        NSMutableArray *arrayNuMm = [NSMutableArray array];

        for (int j = 0; j <  8; j ++) {

        

            [arrayNuMm addObject:[NSString stringWithFormat:@"%c————%d",i,j]];//每一組的行數

        }

        [_contentArray addObject:arrayNuMm];// 26組

        [_switchArray addObject:[NSNumber numberWithBool:NO]];// 默認爲NO

    }

}

 

@end

相關文章
相關標籤/搜索