zoukankan      html  css  js  c++  java
  • UI_UITableView_搭建

    创建 tableView

    UITableViewStyle 有两种选择

    #pragma mark - 创建 tableView
    - (void)createTableView
    {
        // 枚举类型共同拥有两个
        self.mainTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        [self addSubview:self.mainTableView];
    }

    根视图控制器遵守协议

    @interface RootViewController () <UITableViewDataSource, UITableViewDelegate>
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // 设置数据源代理
        self.rootView.mainTableView.dataSource = self;
    
        // 设置 delegate
        self.rootView.mainTableView.delegate = self;
    
        self.title = @"联系人";
    
    }

    重写代理方法

    返回分组的个数

    #pragma -mark 返回分组的个数
    // 默觉得 1
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 2;
    }

    每一个分组显示多少行数据 必须

    #pragma mark - 每一个分组显示多少行数据 *必须*
    // dataSource 下方法
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.nameArray.count;
    }

    每行显示什么内容 必须

    #pragma mark - 每行显示什么内容 *必须*
    // dataSource 下方法
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        // 四种类型
        // UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    
    
        // static 作用
        /**
         *  1、清零功能 static int a;
         *  2、保值作用
         *  3、隐藏功能 
         */
    
        // 静态变量 保值作用 仅仅创建一次
        static NSString *cell_id = @"UITableViewCell";
        // 利用重用创建
        UITableViewCell *cell = nil;
    
        // 在重用池查找
        cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
    
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell_id];
        }
    
    
    
        cell.textLabel.text = self.nameArray[indexPath.row];
    
    //    indexPath.section  那个分组
    //    indexPath.row    哪一行
        NSString *name = [NSString stringWithFormat:@"%ld.png", indexPath.row + 1];
        cell.imageView.image = [UIImage imageNamed:name];
    
        // 显示具体信息 cell 类型用 UITableViewCellStyleSubtitle
        cell.detailTextLabel.text = self.numberArray[indexPath.row];
    
        // 右側附件button 枚举类型
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
        return cell;
    }

    row 的高度

    #pragma mark - 跳转 row 的高度
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 80.0f;
    }
    

    返回分组的名字

    #pragma mark - 返回分组的名字
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        if (section == 0) {
            return @"分组一";
        }
        return @"分组二";
    }

    设置分组头部

    #pragma mark - 设置分组头部
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor redColor];
        if (section == 0) {
            label.text = @"1组";
        } else {
            label.text = @"2组";
        }
        return label;
    }

    设置分组头部的高度

    #pragma mark - 设置分组头部的高度
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 60.0f;
    }#pragma mark - 设置分组头部的高度
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 60.0f;
    }

    cell 的点击事件

    #pragma mark - cell 的点击事件
    - (void)tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // 取消选中状态
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
        SecondViewController *secondVC = [[SecondViewController alloc] init];
    
        [self.navigationController pushViewController:secondVC animated:YES];
    }

    实现索引

    #pragma mark - 实现索引
    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
    {
        return @[@"1组", @"2组"];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
  • 相关阅读:
    安装jupyter_contrib_nbextensions库
    1.20
    架构之美阅读笔记01
    使用 netcat 数据源测试 Flume
    使用 Avro 数据源测试 Flume
    Tensorflow01-认识张量Tensor
    Spark06-RDD分区、缓存与Checkpoint讲解
    Spark05-RDD算子细谈
    Spark04-RDD入门
    Spark03-Scala面向对象和函数式编程
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/7255702.html
Copyright © 2011-2022 走看看