UITableView基础

作者: 非典型技术宅 | 来源:发表于2016-11-09 21:52 被阅读240次

    1. UITableView的作用

    • 以垂直滚动方式显示数据列表。
    • UITableView 的两种内置样式:UITableViewStylePlain 和UITableViewStyleGrouped 。
    • tableView只是一个愚蠢的容器,只负责显示。显示的是什么自己完全不知道。
    • 数据都是由dataSource提供。
      要满足快速滚动,性能很重要

    2. UITableView的常用属性

    2.1 分割线属性

    属性名称 作用
    separatorStyle 分隔线样式
    separatorColor 分隔线颜色

    2.2 选中属性

    属性名称 作用
    allowsSelection 允许选中
    allowsMultipleSelection 允许多选

    2.3 行数

    属性名称 作用
    indexPathsForSelectedRows 当前选中行数
    indexPathsForVisibleRows 当前可见行数

    2.4 背景

    属性名称 作用
    backgroundView 背景视图
    selectedBackgroundView 选中时的背景视图

    2.5 UITableViewCell的selectionStyle属性可设置被选中时的背景颜色

    属性名称 作用
    UITableViewCellSelectionStyleNone 没有颜色
    UITableViewCellSelectionStyleBlue 蓝色(默认)
    UITableViewCellSelectionStyleGray 灰色

    3. tableView展示数据三部曲

    1. 遵守数据源协议;
    2. 设置数据源
    3. 实现相应数据源方法
    • cell的默认高度是44,宽度和tableView等宽。

    3.1 遵守数据源

    @interface ViewController ()<UITableViewDataSource>
    

    3.2 设置数据源

        self.tableView.dataSource = self;
    

    3.3 实现数据源方法

    1. 总共多少组
    2. 每组多少行
    3. 每组中每行的内容
    //返回有多少组
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 2;
    }
    
    //返回有多少行,section 组的索引
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return 10;
    }
    
    //返回每一组的每一行显示什么内容
    - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        
        //创建UITableViewCell
        UITableViewCell *tableViewCell = [[UITableViewCell alloc]init];
        
        return tableViewCell;
    }
    

    4. UITableViewStyleGrouped样式

    4.1 使用storyboard设置

    Paste_Image.png

    4.2 使用代码设置

    Paste_Image.png
    • UITableViewStyle的style的属性是一个只读属性,所以修改不了。
    • 默认创建出来就是UITableViewStylePlain。
    • 但是可以在初始化的时候直接定义。
        UITableView *haha = [UITableView alloc]initWithFrame:<#(CGRect)#> style:(UITableViewStyle)];
    

    5. UITableViewCell

    5.1 四种默认样式

    Paste_Image.png
    typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
        UITableViewCellStyleDefault,    // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
        UITableViewCellStyleValue1,     // Left aligned label on left and right aligned label on right with blue text (Used in Settings)
        UITableViewCellStyleValue2,     // Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
        UITableViewCellStyleSubtitle    // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
    };             // available in iPhone OS 3.0
    

    满足不了需求,就需要自定义样式。

    相关文章

      网友评论

        本文标题:UITableView基础

        本文链接:https://www.haomeiwen.com/subject/sxutpttx.html