zoukankan      html  css  js  c++  java
  • iOS开发——二级列表

      原理很简单,一级菜单放在viewForHeaderInSection里面,加一个点击事件,然后判断它的二级菜单(cell)显不显示。

      直接上代码吧!

    //

    //  HeheTableViewController.m

    //  Demo-tableView

    //

    //  Created by yyt on 16/5/13.

    //  Copyright © 2016年 yyt. All rights reserved.

    //

    #import "HeheTableViewController.h"

    #define klScreenWidth self.view.bounds.size.width

    @interface HeheTableViewController ()

    @property(nonatomic,strong) NSArray *dataArr;

    @property(nonatomic,strong) NSMutableArray *tapButtons;

    @property(nonatomic,strong) NSMutableArray *openedIndexArr;

    @end

    @implementation HeheTableViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        _dataArr = @[@{@"name":@"A区",

                       @"arr":@[@"A1",@"A2",@"A3",@"A4"]

                       },

                     @{@"name":@"B区",

                       @"arr":@[@"B1",@"B2",@"B3"]

                       },

                     @{@"name":@"C区",

                       @"arr":@[@"C1",@"C2",@"C3",@"C4",@"C5",@"C6"]

                       },

                     @{@"name":@"D区",

                       @"arr":@[@"D1",@"D2",@"D3",@"D4",@"D5"]

                       }

                     ];

        

        _tapButtons = [NSMutableArray array];

        

        _openedIndexArr = [NSMutableArray array];

    }

    #pragma mark - TableViewDataSource

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

        return _dataArr.count;

    }

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

        NSString *str = [NSString stringWithFormat:@"%ld",section];

        if ([_openedIndexArr containsObject:str]) {

            NSDictionary *dict = _dataArr[section];

            NSArray *arr = dict[@"arr"];

            return arr.count;

        }

        return 0;

    }

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

        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];

        

        NSString *str = [NSString stringWithFormat:@"%ld",indexPath.section];

        

        if ([_openedIndexArr containsObject:str]) {

            NSDictionary *dict = _dataArr[indexPath.section];

            NSArray *arr = dict[@"arr"];

            cell.textLabel.text = arr[indexPath.row];

        }

        

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        

        return cell;

    }

    #pragma mark - TableViewDelegate

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

        return 50;

    }

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

        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, klScreenWidth, 50)];

        bgView.backgroundColor = [UIColor greenColor];

        

        NSDictionary *dict = _dataArr[section];

        NSString *title = dict[@"name"];

        

        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, klScreenWidth-40, 30)];

        titleLabel.text = title;

        titleLabel.textColor = [UIColor whiteColor];

        titleLabel.font = [UIFont systemFontOfSize:22];

        [bgView addSubview:titleLabel];

        

        UIButton *tapButton = [UIButton buttonWithType:UIButtonTypeCustom];

        tapButton.frame = bgView.frame;

        [tapButton addTarget:self action:@selector(openOrCloseSectionView:) forControlEvents:UIControlEventTouchUpInside];

        [_tapButtons addObject:tapButton];

        [bgView addSubview:tapButton];

        

        return bgView;

    }

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

        NSInteger index = [_tapButtons indexOfObject:sender];

        NSString *str = [NSString stringWithFormat:@"%ld",index];

        if ([_openedIndexArr containsObject:str]) {

            [_openedIndexArr removeObject:str];

        } else {

            [_openedIndexArr addObject:str];

        }

        [_tapButtons removeAllObjects];

        [self.tableView reloadData];

    }

    @end

  • 相关阅读:
    KMP算法
    字典树
    BM算法
    AC自动机
    系统架构师基础到企业应用架构服务层
    系统架构师基础到企业应用架构系统设计规范与原则[上篇]
    系统架构师基础到企业应用架构系统建模[中篇](下)
    系统架构师基础到企业应用架构企业应用架构
    系统架构师基础到企业应用架构客户端/服务器
    系统架构师基础到企业应用架构系统设计规范与原则[下篇]
  • 原文地址:https://www.cnblogs.com/yyt-hehe-yyt/p/5478098.html
Copyright © 2011-2022 走看看