http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 2484|回复: 7

[新手开发之旅] iOS新手开发之旅-初识列表视图UITableView

[复制链接]
发表于 2018-12-3 17:43:48 | 显示全部楼层 |阅读模式
本帖最后由 竹林风 于 2018-12-4 10:59 编辑

文章导航  

【iOS新手开发之旅】   http://www.sufeinet.com/thread-24000-1-1.html

内容摘要

  • 初始化UITableView
  • UITableView如何显示数据
  • UITabelViewCell结构
  • cell的重用原理(重难点)
  • UITableView&UITableViewCell的常见设置
  • UITableView数据刷新方法&数据刷新的原则


初始化UITableView
[Objective-C] 纯文本查看 复制代码
 //    tableView是一个用户可以滚动的多行单列列表,在表视图中,每一行都是一个UITableViewCell对象,表视图有两种风格可选:
    //    typedef NS_ENUM(NSInteger, UITableViewStyle) {
//    UITableViewStylePlain,          // regular table view
//    UITableViewStyleGrouped         // preferences style table view
//};
    UITableView *tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
//    设置数据源代理
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];


UITableView如何显示数据


[Objective-C] 纯文本查看 复制代码
//数据源代理方法

/**
 *  告诉tableView一共有多少组数据,默认为1组
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

/**
 *  告诉tableView第section组有多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 5;
}

/**
 *  告诉tableView第indexPath行显示怎样的cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 0.重用标识
    // 被static修饰的局部变量:只会初始化一次,在整个程序运行过程中,只有一份内存
    static NSString *ID = @"cell";
    
    // 1.先根据cell的标识去缓存池中查找可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 2.如果cell为nil(缓存池找不到对应的cell)
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    // 3.覆盖数据
    cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];
    
    return cell;
}

/**
 *  告诉tableView第section组的头部标题
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    
    return @"头部标识";
}

/**
 *  告诉tableView第section组的尾部标题
 */
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    
    return @"尾部标题";
}



UITabelViewCell结构


10401961-e4163c0c0ca48d95.png

Cell重用原理


  • iOS设备的内存有限,如果用UITableView显示成千上万条数据,就需要成千上万个UITableViewCell对象的话,那将会耗尽iOS设备的内存。要解决该问题,需要重用UITableViewCell对象
  • 重用原理:当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免创建新对象
  • 还有一个非常重要的问题:有时候需要自定义UITableViewCell(用一个子类继承UITableViewCell),而且每一行用的不一定是同一种UITableViewCell,所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的UITableViewCell,那么UITableView在重用UITableViewCell时可能会得到错误类型的UITableViewCell
  • 解决方案:UITableViewCell有个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化一个UITableViewCell对象


UITableViewCell的常见设置


[Objective-C] 纯文本查看 复制代码
// 0.重用标识
    // 被static修饰的局部变量:只会初始化一次,在整个程序运行过程中,只有一份内存
    static NSString *ID = @"cell";
    
    // 1.先根据cell的标识去缓存池中查找可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 2.如果cell为nil(缓存池找不到对应的cell)
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }



UITableView数据刷新方法&数据刷新的原则


重新刷新屏幕上的所有cell


[Objective-C] 纯文本查看 复制代码
[self.tableView reloadData];


刷新特定行的cell


[Objective-C] 纯文本查看 复制代码
[self.tableView reloadRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];


插入特定行数的cell


[Objective-C] 纯文本查看 复制代码
[self.tableView insertRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];


删除特定行数的cell


[Objective-C] 纯文本查看 复制代码
[self.tableView deleteRowsAtIndexPaths:@[
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0]
        ]
        withRowAnimation:UITableViewRowAnimationLeft];


数据刷新的原则
  • 通过修改模型数据,来修改tableView的展示先修改模型数据
  • 再调用数据刷新方法
  • 改模型<==>改界面
  • 不要直接修改cell上面子控件的属性



看效果:
0001B45B0CC5A3205DDC810DDB926465.png

附件: Test-UITableView.zip (101.27 KB, 下载次数: 0)


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2018-12-3 17:50:08 | 显示全部楼层
还有附件可以下载,好给力
 楼主| 发表于 2018-12-3 17:52:20 | 显示全部楼层
UITableView的常见设置
[Objective-C] 纯文本查看 复制代码
// 分割线颜色
self.tableView.separatorColor = [UIColor redColor];

// 隐藏分割线
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
 楼主| 发表于 2018-12-3 17:52:52 | 显示全部楼层
UITableViewCell的常见设置

[Objective-C] 纯文本查看 复制代码
// 取消选中的样式
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// 设置选中的背景色
UIView *selectedBackgroundView = [[UIView alloc] init];
selectedBackgroundView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = selectedBackgroundView;

// 设置默认的背景色
cell.backgroundColor = [UIColor blueColor];

// 设置默认的背景色
UIView *backgroundView = [[UIView alloc] init];
backgroundView.backgroundColor = [UIColor greenColor];
cell.backgroundView = backgroundView;

// backgroundView的优先级 > backgroundColor
// 设置指示器
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.accessoryView = [[UISwitch alloc] init];
发表于 2018-12-3 19:22:21 | 显示全部楼层
我只是路过打酱油的。
发表于 2018-12-3 20:31:11 | 显示全部楼层
强烈支持楼主ing……
发表于 2018-12-3 20:31:35 | 显示全部楼层
楼主帖子写的不错, 文本加图片,还有源码下载, 相当给力, 加油
发表于 2018-12-3 20:32:57 | 显示全部楼层
真是难得给力的帖子啊。
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-3-29 21:52

© 2014-2021

快速回复 返回顶部 返回列表