zoukankan      html  css  js  c++  java
  • UITableViewController

    1、tableView 的创建与设置

    UITableView *tableView = [[UITableView alloc] init];
    /*
    带显示类型的设置
    UITableViewStylePlain,     // 简单模式,每个分段之间紧密连接,头脚标题悬浮显示,默认类型
    UITableViewStyleGrouped    // 分组模式,每个分段之间分开,头脚标题跟随移动,头标题英文自动大写
    */
    //UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
    // 将 tableView 添加到窗口中
    [self addSubview:tableView];
    // 设置位置尺寸
    tableView.frame = self.bounds;
    // 设置 tableView 的代理
    tableView.delegate = self;
    tableView.dataSource = self;
    
    // 设置表格的分割线颜色:设置为 clearColor 时即可隐藏(不显示)所有分割线
    tableView.separatorColor = [UIColor redColor];
    // 自定义表格分割线:系统分割线的左边无法紧靠表格左边框,隐藏系统分割线,自定义视图,添加到 Cell 的下边实现,同时可以清除表格在 UITableViewStylePlain 类型时的多余分割线
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 设置表格的背景
    // 设置表格的背景视图
    UIImageView *bgImageV = [[UIImageView alloc] init];
    tableView.backgroundView = bgImageV;
    // 设置表格的背景颜色
    tableView.backgroundColor = [UIColor blueColor];
    
    /*
    * 清除表格多余的分割线
    * 表格为 UITableViewStylePlain 类型时,若表格的内容没有占满屏幕时,没有设置内容的部分表格也会有分割线
    * 创建自定义的 view,将该 view 的背景颜色清空(默认为透明),并添加到表格的脚视图上
    */
    // 设置表格分割线左边距为零
    [tableView setSeparatorInset:UIEdgeInsetsZero];
    [tableView setLayoutMargins:UIEdgeInsetsZero];
    
    // 设置全部行的高度,默认为 44
    tableView.rowHeight = 60;
    // 设置估计行高: 设置全部行的高度
    tableView.estimatedRowHeight = 80;
    // 设置自动计算行高
    tableView.rowHeight = UITableViewAutomaticDimension;
    
    // 打开表格的编辑模式
    tableView.editing = YES;
    // 翻转表格的编辑模式
    tableView.editing = !tableView.editing;
    // 翻转表格的编辑模式
    [tableView setEditing:!tableView.editing animated:YES];
    // 设置表格普通模式下是否允许单选
    tableView.allowsSelection = YES;
    // 设置表格在编辑模式下是否允许单选
    tableView.allowsSelectionDuringEditing = YES;
    // 设置表格普通模式下是否允许多选
    tableView.allowsMultipleSelection = YES;
    // 设置表格在编辑模式下是否允许多选
    tableView.allowsMultipleSelectionDuringEditing = YES;
    
    // 设置表格的分割线边距:上、左、下、右,只有左、右设置有效
    tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
    
    // 设置表格的表头视图: 只有视图的高度设置起作用
    UIView *headerView = [[UIView alloc] init];
    tableView.tableHeaderView = headerView;
    // 设置表格的表尾视图: 只有视图的高度设置起作用
    UIView *footerView = [[UIView alloc] init];
    tableView.tableFooterView = footerView;
    
    // 取消表格选择: 在表格选中协议方法中设置,表格点击变色后恢复原来颜色,设置后无法实现表格多选
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    

    2、代理方法

    • 遵守 UITableViewDelegate, UITableViewDataSource 协议
    // 设置分段数
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        // 数据源数组为多维数组时,用数组计算
        return self.dataArray.count;
    }
    
    // 设置行数
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // section 段,返回每段中有多少行
        return [[self.dataArray objectAtIndex:section] count];
    }
    
    //可单独设置每一行的高度,默认为 44
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return 60;
    }
    
    // 协议方法设置估计行高:可单独设置每一行的估计行高
    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
        return 80;
    }
    
    // 设置每一行显示的内容
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        // 创建标识词,随意设置,但不能和其它 tableView 的相同
        static NSString *indentifier = @"testIdentifier";
        // 根据标志词先从复用队列里查找
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
        // 复用队列中没有时再创建
        if (cell == nil) {
            // 创建新的 cell,默认为主标题模式
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
        }
    
        // 设置每一行显示的文字内容
        cell.textLabel.text = [[myDataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        // indexPath.section 分段数,indexPath.row 行数,设置图片内容,图片在 Cell 的左端,图片大小自动压缩
        cell.imageView.image = [UIImage imageNamed:@"HQ_0003"];
    
        return cell;
    }
    
    
    // 设置分段的头标题高度,UITableViewDelegate 协议方法
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
        return 40;
    }
    
    // 设置分段的脚标题高度,UITableViewDelegate 协议方法
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
        return 30;
    }
    
    // 设置分段的头标题内容,UITableViewDataSource 协议方法
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        if (0 == section) {
            return @"English Header";
        }
        else {
            return @"中文 Header";
        }
    }
    
    // 设置分段的脚标题内容,UITableViewDataSource 协议方法
    - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
        if (0 == section) {
            return @"English Footer";
        }
        else {
            return @"中文 Footer";
        }
    }
    
    // 分段头标题视图,UITableViewDelegate 协议方法,返回自定义的标题视图
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
        if (!section) {
            label.text = @"English Header";
        }
        else {
            label.text = @"中文 Header";
        }
    
        return label;
    }
    
    // 分段脚标题视图,UITableViewDelegate 协议方法
    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
        if (!section) {
            label.text = @"English Footer";
        }
        else {
            label.text = @"中文 Footer";
        }
    
        // 返回自定义的标题视图
        return label;
    }
    
    // 设置表格的分割线
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        [cell setSeparatorInset:UIEdgeInsetsZero];
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    

    3、设置方法

    • 3.1 重载表格视图

    // 重走所有的表格视图方法,刷新所有的表格
    [tableView reloadData];
    
    // 重载某一分段
    [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];  
    
    // 重载某一个行
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
    // 删除一个 cell: 只刷新删除的 cell
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
    // 插入一个 cell: 只刷新插入的 cell
    [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    
    • 3.2 获取 cell

    // 获取指定行的 cell
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
    
    // 获取所有被选中的行
    NSArray *indexPaths = [tableView indexPathsForSelectedRows];
    

    4、Cell 的创建与设置

    • 使用 dequeueReuseableCellWithIdentifier: 可不注册,但是必须对获取回来的 cell 进行判断是否为空,若空则手动创建新的 cell;

    • 使用 dequeueReuseableCellWithIdentifier: forIndexPath: 必须注册,但返回的 cell 可省略空值判断的步骤。

    • tableViewCell 的复用机制:

      • 当一个 cell 滑出屏幕的时候,会被放到复用队列里(系统自动操作)。
      • 当一个 cell 即将出现的时候,我们需要先从复用队列里查找,找到就直接用,找不到就创建。
    • 自定义Cell的流程

    • 4.1 设置 Cell 的类型

    /*
    UITableViewCellStyleDefault,  // 可选图片 + 主标题模式,默认
    UITableViewCellStyleValue1,   // 可选图片 + 左右主副标题模式,两端对齐
    UITableViewCellStyleValue2,   // 左右主副标题模式,中间对齐
    UITableViewCellStyleSubtitle  // 可选图片 + 上下主副标题模式
    */
    
    // 主标题模式,默认类型
    cell = [[UITableViewCell alloc] init];
    
    // 设置类型
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"testIdentifier"];
    
    // Cell 附属控件类型的设置
    /*
    如果附属控件里有 button ,这个 button 会独立出来
    
    UITableViewCellAccessoryNone,                    // 无附属控件,默认
    UITableViewCellAccessoryDisclosureIndicator,     // 箭头,不能点击
    UITableViewCellAccessoryDetailDisclosureButton,  // 详情按钮和箭头,可以点击
    UITableViewCellAccessoryCheckmark,               // 对号,不能点击
    UITableViewCellAccessoryDetailButton             // 详情按钮,可以点击
    */
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    
    // Cell 附属控件视图的设置
    /*
    设置自定义视图为 Cell 的附属控件,需设置 view 的大小
    */
    cell.accessoryView = myAccessoryView;
    
    • 4.2 设置 Cell 的背景视图

    // Cell 的背景视图设置
    /*
    设置自定义视图为 Cell 背景视图
    图片大小自动压缩填充
    */
    cell.backgroundView = myBackgroundView;
    
    // Cell 选中时的背景视图设置
    cell.selectedBackgroundView = myBackgroundView;
    
    // Cell 背景颜色的设置
    cell.backgroundColor = [UIColor yellowColor];
    
    // 设置 cell 被点击时的颜色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    // 取消表格选择变色
    // 在表格选中协议方法中设置,表格点击变色后恢复原来颜色,设置后无法实现表格多选
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    

    5、自定义tableView区

  • 相关阅读:
    js入门2计算平方
    js入门1
    html入门
    表单的例
    sql概述
    关联规则
    回归分析——logic回归
    聚类分析——动态聚类(K-means)
    聚类分析——层次聚类
    创建一个用递归算法求a的b次方的函数
  • 原文地址:https://www.cnblogs.com/CH520/p/9313198.html
Copyright © 2011-2022 走看看