zoukankan      html  css  js  c++  java
  • IOS中TableView的用法

    一、UITableView

    1.数据展示的条件

    1> UITableView的所有数据都是由数据源(dataSource)提供的,所以要想在UITableView展示数据,必须设置UITableViewdataSource数据源对象

    2> 要想当UITableViewdataSource对象,必须遵守UITableViewDataSource协议,实现相应的数据源方法

    3> UITableView想要展示数据的时候,就会给数据源发送消息(调用数据源方法),UITableView会根据方法返回值决定展示怎样的数据

    2.数据展示的过程

    1> 先调用数据源的

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    得知一共有多少组

    2> 然后调用数据源的

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

    得知第section组一共有多少行

    3> 然后调用数据源的

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

    得知第indexPath.section indexPath.row 行显示怎样的cell(显示什么内容)

    3.常见数据源方法

    1> 一共有多少组

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    2> section组一共有多少行

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

    3> indexPath.section indexPath.row行显示怎样的cell(显示什么内容)

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

    4> section组显示怎样的头部标题

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

    5> section组显示怎样的尾部标题

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

    4.tableView刷新数据的方式

    1> 修改模型数据

    2> 刷新表格

    * reloadData 整体刷新(每一行都会刷新)

    * - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

    局部刷新

    5.性能优化

    1> 定义一个循环利用标识

    static NSString *ID = @"C1";

    2> 从缓存池中取出可循环利用的cell

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    3> 如果缓存池中没有可循环利用的cell

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

    }

    4> 覆盖cell上面的数据

    cell.textLabel.text = [NSString stringWithFormat:@"%d行数据", indexPath.row];

    如果你错过了一天,那么你就真的错过了一天……ues.hk
  • 相关阅读:
    zipkin启动报错(Caused by: java.lang.ClassNotFoundException: zipkin.Component)的解决方法
    Java中的long与double的区别
    redis使用笔记
    解决node编程频繁修改代码,需要重启服务器问题
    远程连接mysql要点 虚拟主机定义与分类
    详析静态网站与动态网站区别(服务器ip dns 端口)
    JavaEE-实验四 HTML与JSP基础编程
    JavaEE-实验三 Java数据库高级编程
    JavaEE-实验二 Java集合框架实验
    mysql中文乱码 常见编码问题解决方法分享
  • 原文地址:https://www.cnblogs.com/myios/p/3670694.html
Copyright © 2011-2022 走看看