iOS 开发学习之 User Interface(9)UITableView

1. UITableView

表格视图(确切的说是列表视图,多行一列)。继承自UIScrollView.

2. UITableView使用

    > 属性,方法

    [UITableView alloc] initWithFrame:style:]   // 以样式,尺寸信息初始化. 表格样式分为普通UITableViewStylePlain,分区UITableViewStyleGrouped。

    .separatorStyle    // 分隔线样式,可设置为无样式UITableViewCellSeparatorStyleNone,  线条样式 UITableViewCellSeparatorStyleSingleLine, 线条雕刻样式 UITableViewCellSeparatorStyleSingleLineEtched 此样式只对分组表格有效

    .separatorColor   // 分隔线颜色

    .separatorInset    // 分隔线外间距            

    // 分区索引

    .sectionIndexColor   // 分区索引的前景色

    .sectionIndexBackgroundColor  // 分区索引的背景色


    .dataSource        //  数据源代理对象,主要处理表格数据的显示及少量表格行为。数据源协议名称 UITableViewDataSource

    .delegate            //  行为协议代理对象,主要处理表格行为

     > dataSource 协议方法

     必须要实现的

//  返回一个表格中其中一组内的行数

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

//  返回一个单元格,即要显示在表格中每一行上的内容

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

                

        /* indexPath 索引路径 , 在一个嵌套数组中,指向一个特定节点的路径。

            UITableViewCell的复用技术:只创建屏幕可见区域内的单元格,在向上,向下滑动时,已不可见的单元格会放入回收池中,新数据要显示时,所需单元格不再重新创建,而是从回收池中获取 

            复用技术的代码: 

            [tableView dequeueReusableCellWithIdentifier:] 此种方式下,需判断是否拿到可复用的单元格. 可设置单元格样式。

            [tableView dequeueReusableCellWithIdentifier: forIndexPath:] 此方式下,无需判断是否拿到可复用单元格,但需要注册标识符: [tableView registerClass: forCellReuseIdentifier:] or [tableView registerNib: forCellReuseIdentifier:]

       */


     可选实现的

// 返回表格中有几个分区

     - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

        // 返回每个分区的表头文字

        - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

        // 返回每个分区的底部文字

        - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

// 返回所有分区索引标题     

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView

  // 设置索引标题对应的分区

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index

     > delegate 协议方法

     // 每个单元格(行)的高度

     - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

     // 自定义每个分区的表头视图

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

     // 自定义每个分区的底部视图

     - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

       // 单元格被选中

       - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

       // 单元格被反选

       - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

       // 滑动后删除或添加被点击后执行方法

       - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

       // 删除按钮上的文字

       - (nullable NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

       // 左滑后将要显示删除等编辑按钮前

       - (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath

       // 编辑结束后

       - (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath 

       // 添加,移动


5. 因为应用是在沙箱(sandbox)中的,在文件读写权限上受到限制,只能在几个目录下读写文件:

  • Documents:应用中用户数据可以放在这里,iTunes备份和恢复的时候会包括此目录
  • tmp:存放临时文件,iTunes不会备份和恢复此目录,此目录下文件可能会在应用退出后删除
  • Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除
——————————————————————————————

1. UITableViewStyleGrouped 分区表格样式创建表格

   .separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;  // 雕刻,双线效果。 测试无效

2. UITableView的其他方法属性

    // 表格是否进入编辑模式 ,用以显示添加,删除,移动标识

    setEditing:   // 添加,删除不能同时显示

    .allowsSelectionDuringEditing   // 在编辑模式下是否允许可点击选择

    // 返回编辑模式下,每行左侧显示的是删除还是添加

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

     //  在可处理删除/添加方法中判断每行是何种操作 在tableviewdatasource协议下

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

    //  行被移动后处理方法 在tableviewdatasource协议下

    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

    //  自ios8开始支持的:在每行左滑或者编辑模式下点左侧删除按钮,右侧滑出多个按钮

    - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

3.  折叠分区内容

     在分区样式下设置多个分区,每个分区对应不同的数据源



4.  UITableViewCell 定制

     定制分为三种方式:纯代码,xib,storyboard

     > 纯代码

     -自定义类继承自UITableViewCell

     -添加子视图控件属性,以使其可通过cell.方式可获取

     -重写父类方法 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier  // 复用时单元格初始化调用此方法,在其内实现初始化子视图,将子视图添加到cell.contentView中

/*** 开始- UITableViewCell 自适应内容高度 ***/

      对于纯文字的情况(若还有其他,如图片,高度累加即可)

      - 计算文字在给定宽度下所占用的尺寸

          # 设定或获取文字的属性

          # 计算在给定文字属性,绘制模式下,文字的高度

              [string boundingRectWithSize:CGSizeMake(300, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil]

      - 在返回行高协议方法中,返回计算的高度,根据实际情况可累加固定值

      - 在绑定单元格内容的协议方法中,获取计算的高度,并通过此设置子视图的尺寸

/*** 结束- UITableViewCell 自适应内容高度 ***/

     > xib 定制cell

     - 自定类继承自UITableViewCell 并选择创建xib文件的选项

     - 拖曳视图控件到xib并创建外联属性与其关联

     - 选择此cell, Xcode右侧属性窗口下Table View Cell下设置 Identifier, 用于单元格复用的标识

     - 若使用方式二复用,则注册时,选择registerNib:forCellReuseIdentifier: 

     - 其余步骤与之前基本一致


     > storyboard 定制cell

——————————————————————————————

1. 可视化定制UITableViewCell

   > xib 定制cell

     - 自定类继承自UITableViewCell 并选择创建xib文件的选项

     - 拖曳视图控件到xib并创建外联属性与其关联

     - 选择此cell, Xcode右侧属性窗口下Table View Cell下设置 Identifier, 用于单元格复用的标识

     - 若使用方式二复用,则注册时,选择registerNib:forCellReuseIdentifier: 

     - 其余步骤与之前基本一致

***** 特别注意:使用xib下定制cell时,若需改变其子视图的frame,则不能设置auto layout选项 *****

    > storyboard 定制cell

    分两种方式:

    方式一:当在storyboard下可视化创建UIViewController时,若需使用定制的UITableViewCell ,仍然可以使用xib方式定制

    方式二: 

    -创建继承 自UITableViewCell的子类,不选择创建xib文件的选项

-在.h文件中声明外联视图属性变量

    -拖曳UITableView到Storyboard 的某个ViewController 后,可拖曳UITableViewCell 到UITableView中,使其作为prototype cell(原型单元格/模版单元格),相当于cell 的xib文件。

    -在UITableViewCell的contentView上拖曳其子视图控件

    -选中UITableViewCell, 右侧属性窗口设置Identifier 用于复用,并设置其class为第一步创建的类名

    -在UITableViewCell中,选择某一视图控件,与第二部创建的外联属性关联

    - 其余步骤与之前相同

2. UITableView中多选删除

    -设置UITableView 可多选

    - 临时数组的使用

    注意:深拷贝,浅拷贝

3. UITableView搜索

    UISearchDisplayController使用(自iOS8开始被弃用

    

    UISearchController 使用(iOS8新增



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值