zoukankan      html  css  js  c++  java
  • 可折叠tableView

     我的xcode 是    IOS9.0    Xcode.7.0

    版本符合,可复制黏贴直接用

    #import "ViewController.h"

     1.定义成员变量 遵循代理

    @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

    {

        NSMutableArray *_switchArray;

        NSMutableArray *_contentArray;

        UITableView *_tableView;

    }

    @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

  • 相关阅读:
    Maven+JSP+Servlet+JDBC+Redis+Mysql实现的黑马旅游网
    Thymeleaf+SpringBoot+SpringDataJPA实现的中小医院信息管理系统
    Thymeleaf+SpringBoot+Mybatis实现的家庭财务管理系统
    JSP+Struts2+JDBC+Mysql实现的校园宿舍管理系统
    JSP+SSH+Mysql+C3P0实现的传智播客网上商城
    JSP+SSH+Mysql实现的学生管理系统
    JSP+Servlet+C3P0+Mysql实现的简单新闻系统
    笔记:EF出现列名 'Discriminator' 无效、类没有加入数据库上下文也被数据迁移生成表
    判断字符串中是否存在的几种方案:string.indexof、string.contains、list.contains、list.any几种方式效率对比
    Sql server 中关闭ID自增字段(SQL取消ID自动增长)
  • 原文地址:https://www.cnblogs.com/fanwenzheIOS/p/4972395.html
Copyright © 2011-2022 走看看