(GeekBand) iOS编程第三周

UITableView数据源

 // 设置数据源
  self.tableView.dataSource = self;
#pragma mark - <UITableViewDataSource>
/**
 *  告诉tableView第section组有多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

/**
 *  告诉tableView一共有多少组数据
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

/**
 *  告诉tableView第indexPath行显示怎样的cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

/**
 *  告诉tableView第section组的头部标题
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

/**
 *  告诉tableView第section组的尾部标题
 */
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

字典 模型转换

tableview中如果每个cell中显示的数据属性都相同 这个时候我们需要把网络数据或者本地数据转换到模型中 直接把模型传递给tableview的cell

//比如 我们这里每个cell需要显示人的名字,头像,简介,那么我们简历一个person模型
person.h
/** 姓名 */
@property (nonatomic, strong) NSString *name;
/** 图标 */
@property (nonatomic, strong) NSString *icon;
/** 简介 */
@property (nonatomic, strong) NSString *intro;
//提供一个便捷构造方法
+ (instancetype)personWithDict:(NSDictionary *)dict;
person.m(这里假设加载本地plist数据)
+ (instancetype)personWithDict:(NSDictionary *)dict
{
    ZJYPerson *hero = [[self alloc] init];
//kVC传值 如果遇到数据名称比较特殊 比如 id 这个时候我们需要做一个映射 把传递过来的id数据传递给定义的ID
//不然KVC会出错 因为在OC中id比较特殊
    [hero setValuesForKeysWithDictionary:dict];
    return hero;
}
@property (nonatomic, strong) NSArray *persons;
//懒加载persons 保证只加载一次
- (NSArray *)persons
{
    if (_persons == nil) {
        // 加载plist中的字典数组
        NSString *path = [[NSBundle mainBundle] pathForResource:@"persons.plist" ofType:nil];
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        // 字典数组 -> 模型数组
        NSMutableArray *personArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            ZJYPerson *person = [ZJYPerson personWithDict:dict];
            [personArray addObject:hero];
        }
        _persons =personArray;
    }
    return _persons;
}

//这个时候 tableview的加载就相当轻松了 
// 默认就是1组 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.persons.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    
    ZJYPerson *hero = self.persons[indexPath.row];
    
    cell.textLabel.text = person.name;
    cell.imageView.image = [UIImage imageNamed:person.icon];
    cell.detailTextLabel.text = person.intro;
    return cell;
}

cell优化

因为在tableview滚动过程中 滚出screen的cell会被系统自动放回cell缓存池,这个时候我们可以通过给cell添加唯一标识来实现cell重用

tableView性能优化 - cell的循环利用方式1

/**
 *  什么时候调用:每当有一个cell进入视野范围内就会调用
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1. 重用标识
    // 被static修饰的局部变量:只会初始化一次,在整个程序运行过程中,只有一份内存
    static NSString *ID = @"cell";
    // 2.先根据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性能优化 - cell的循环利用方式2

  • 定义一个全局变量
// 定义重用标识
NSString *ID = @"cell";
- 注册某个标识对应的cell类型
// 在这个方法中注册cell
- (void)viewDidLoad {
    [super viewDidLoad];
    // 注册某个标识对应的cell类型
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
}
- 在数据源方法中返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.去缓存池中查找cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 2.覆盖数据
    cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];
    return cell;
}

tableView性能优化 - cell的循环利用方式3

  • 在storyboard中设置UITableView的Dynamic Prototypes Cell


    Snip20150602_152.png
  • 设置cell的重用标识


    Snip20150602_153.png
  • 在代码中利用重用标识获取cell
// 0.重用标识
// 被static修饰的局部变量:只会初始化一次,在整个程序运行过程中,只有一份内存
static NSString *ID = @"cell";
// 1.先根据cell的标识去缓存池中查找可循环利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 2.覆盖数据
cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd", indexPath.row];
return cell;

常见错误将UIViewController当做UITableViewController来用

在开发中我们可能因为自己的疏忽或者其他原因 弄错了
会显示以下报错


Snip20150602_110.png

UITableView的常见设置

// 分割线颜色
self.tableView.separatorColor = [UIColor redColor];
// 隐藏分割线
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// tableView有数据的时候才需要分割线
// 开发小技巧:快速取消分割线
 self.tableView.tableFooterView = [[UIView alloc] init];

UITableViewCell的常见设置

// 取消选中的样式(常用) 让当前 cell 按下无反应
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];

自定义cell

  • 等高的cell

    • storyboard自定义cell
      - 1.创建一个继承自UITableViewCell的子类,比如XXXCell
      - 2.在storyboard中
      - 往cell里面增加需要用到的子控件
      - 设置cell的重用标识
      - 设置cell的class为XXXCell
      - 3.在控制器中
      - 利用重用标识找到cell
      - 给cell传递模型数据
      - 4.在XXXCell中
      - 将storyboard中的子控件连线到类扩展中
      - 需要提供一个模型属性,重写模型的set方法,在这个方法中设置模型数据到子控件上
  • xib自定义cell
    - 1.创建一个继承自UITableViewCell的子类,比如XXXCell
    - 2.创建一个xib文件(文件名建议跟cell的类名一样),比如XXXCell.xib
    - 拖拽一个UITableViewCell出来
    - 修改cell的class为XXXCell
    - 设置cell的重用标识
    - 往cell中添加需要用到的子控件
    - 3.在控制器中
    - 利用registerNib...方法注册xib文件
    - 利用重用标识找到cell(如果没有注册xib文件,就需要手动去加载xib文件)
    - 给cell传递模型数据
    - 4.在XXXCell中
    - 将xib中的子控件连线到类扩展中
    - 需要提供一个模型属性,重写模型的set方法,在这个方法中设置模型数据到子控件上
    - 也可以将创建获得cell的代码封装起来(比如cellWithTableView:方法)

  • 代码自定义cell(使用frame)
    - 1.创建一个继承自UITableViewCell的子类,比如XXXCell
    - 在initWithStyle:reuseIdentifier:方法中
    - 添加子控件
    - 设置子控件的初始化属性(比如文字颜色、字体)
    - 在layoutSubviews方法中设置子控件的frame
    - 需要提供一个模型属性,重写模型的set方法,在这个方法中设置模型数据到子控件
    - 2.在控制器中
    - 利用registerClass...方法注册XXXCell类
    - 利用重用标识找到cell(如果没有注册类,就需要手动创建cell)
    - 给cell传递模型数据
    - 也可以将创建获得cell的代码封装起来(比如cellWithTableView:方法)

  • 代码自定义cell(使用autolayout)
    - 1.创建一个继承自UITableViewCell的子类,比如XXXCell
    - 在initWithStyle:reuseIdentifier:方法中
    - 添加子控件
    - 添加子控件的约束(建议使用Masonry
    - 设置子控件的初始化属性(比如文字颜色、字体)
    - 需要提供一个模型属性,重写模型的set方法,在这个方法中设置模型数据到子控件
    - 2.在控制器中
    - 利用registerClass...方法注册XXXCell类
    - 利用重用标识找到cell(如果没有注册类,就需要手动创建cell)
    - 给cell传递模型数据
    - 也可以将创建获得cell的代码封装起来(比如cellWithTableView:方法)

  • 非等高的cell

    • ** xib自定义cell **
      - 在模型中增加一个cellHeight属性,用来存放对应cell的高度
      - 在cell的模型属性set方法中调用[self layoutIfNeed]方法强制布局,然后计算出模型的cellheight属性值
      - 在控制器中实现tableView:estimatedHeightForRowAtIndexPath:方法,返回一个估计高度,比如200
      - 在控制器中实现tableView:heightForRowAtIndexPath:方法,返回cell的真实高度(模型中的cellHeight属性)
    • storyboard自定义cell
    • 代码自定义cell(frame)
    • 代码自定义cell(Autolayout)

表格中的数据操作

  • 创建一个可变数组来保存数据
    @property (nonatomic, strong) NSMutableArray *deals;
  • 懒加载 deals
  • 代理方法
#pragma mark - TableView代理方法
/**
 * 只要实现这个方法,左划cell出现删除按钮的功能就有了
 * 用户提交了添加(点击了添加按钮)\删除(点击了删除按钮)操作时会调用
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {  // 点击了“删除”
        // 删除模型
        [self.deals removeObjectAtIndex:indexPath.row];
        // 刷新表格
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { // 点击了+
        NSLog(@"+++++ %zd", indexPath.row);
    }
}
/**
 * 这个方法决定了编辑模式时,每一行的编辑类型:insert(+按钮)、delete(-按钮)
 */
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath.row % 2 == 0? UITableViewCellEditingStyleInsert: UITableViewCellEditingStyleDelete;
}
- 进入编辑模式
 - (IBAction)switchEditing {
    [self.tableView setEditing:!self.tableView.isEditing animated:YES];
}

- (IBAction)add {
    // tableView里面需要显示新的cell数据,只需要操作模型数据
   // 这里使用随机数据
    ZJYDeal *deal = [[ZJYDeal alloc] init];
    deal.title = [NSString stringWithFormat:@"XXdeal %d折", arc4random_uniform(50)];
    deal.price = [NSString stringWithFormat:@"%d", 10 + arc4random_uniform(100)];
    deal.buyCount = [NSString stringWithFormat:@"%d", arc4random_uniform(1000)];
    deal.icon = @"xxx";
    [self.deals insertObject:deal atIndex:0];
    ZJYDeal *deal2 = [[ZJYDeal alloc] init];
    deal2.title = [NSString stringWithFormat:@"YYdeal %d折", arc4random_uniform(50)];
    deal2.price = [NSString stringWithFormat:@"%d", 10 + arc4random_uniform(100)];
    deal2.buyCount = [NSString stringWithFormat:@"%d", arc4random_uniform(1000)];
    deal2.icon = @"xxx";
    [self.deals insertObject:deal2 atIndex:0];
    // 提醒tabelView,模型数据发生了变化,请重新识别,请重新向数据源索要数据, 就是刷新数据的意思                         
    [self.tableView reloadData];
    // 插入某些特定的行
//    [self.tableView insertRowsAtIndexPaths:@[
//                                             [NSIndexPath indexPathForRow:0 inSection:0],
//                                             [NSIndexPath indexPathForRow:1 inSection:0]
//                                             ] withRowAnimation:UITableViewRowAnimationLeft];
}
 // 移除模型数据
- (IBAction)remove {
    [self.deals removeObjectAtIndex:0];
    [self.deals removeObjectAtIndex:0];
    [self.deals removeObjectAtIndex:0];
    // 刷新表格
    [self.tableView reloadData];
//移除指定的行
//    [self.tableView deleteRowsAtIndexPaths:@[
//                                             [NSIndexPath indexPathForRow:0 inSection:0],
//                                             [NSIndexPath indexPathForRow:1 inSection:0],
//                                             [NSIndexPath indexPathForRow:2 inSection:0]
//                                             ] withRowAnimation:UITableViewRowAnimationRight];
//修改现有表格数据
- (IBAction)update {
    // 修改模型
    ZJYDeal *deal = self.deals[3];
    deal.price = [NSString stringWithFormat:@"%d", 50 + arc4random_uniform(100)];;
    // 刷新表格
    [self.tableView reloadData];
//修改指定行数据
//    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:0]] withRowAnimation:UITableViewRowAnimationMiddle];
}

批量删除

- (void)viewDidLoad {
    [super viewDidLoad];
    // 允许在编辑模式进行多选操作
    self.tableView.allowsMultipleSelectionDuringEditing = YES;
}
- (IBAction)multiOperation:(id)sender {
//开启编辑模式
    [self.tableView setEditing:!self.tableView.isEditing animated:YES];
}
- (IBAction)remove {
    // 获得所有被选中的行
    NSArray *indexPaths = [self.tableView indexPathsForSelectedRows];
    // 便利所有的行号
    NSMutableArray *deletedDeals = [NSMutableArray array];
    for (NSIndexPath *path in indexPaths) {
        [deletedDeals addObject:self.deals[path.row]];
    }
    // 删除模型数据
    [self.deals removeObjectsInArray:deletedDeals];
   // 刷新表格
    [self.tableView reloadData];
}

表格的更多高级使用主要体现在 QQ,wechat这类的高度自定义cell的软件
具体聊天类app的cell自定义相关的内容,不在这里写了,有时间再整理

这周作业是通讯录
通讯录涉及知识点较多,在另外一篇文章http://www.jianshu.com/p/7ffa4078ac64 有详细描述

CollectionView

主要用于横向或者竖向滑动 常见于app版本更新后的新特性显示,或者app中间的滑动view
说起CollectionView就不得不说 UICollectionViewController

  • UICollectionViewController层次结构:控制器View 上面UICollectionView
  • 初始化的时候必须设置布局参数,通常使用系统提供的流水布局UICollectionViewFlowLayout
  • cell必须通过注册
  • 自定义cell
- (instancetype)init
{
    // 流水布局对象,设置cell的尺寸和位置
    UICollectionViewFlowLayout *layout  = [[UICollectionViewFlowLayout alloc] init];  
    // 设置滚动的方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 设置cell的尺寸
    layout.itemSize = XMGScreenBounds.size;
//    // 设置cell之间间距
    layout.minimumInteritemSpacing = 0;
//    // 设置行距
    layout.minimumLineSpacing = 0;
   // 设置每一组的内间距
//    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10); 
    return  [super initWithCollectionViewLayout:layout];
}

#pragma mark - UICollectionView有多少组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

#pragma mark - 返回第section组有多少个cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

#pragma mark - 返回每个cell长什么样
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
  XXCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    NSString *imageName = [NSString stringWithFormat:@"guide%ldBackground",indexPath.item + 1];
    cell.image = [UIImage imageNamed:imageName];  
    return cell;    
}
//相关属性设置
- (void)viewDidLoad {
    [super viewDidLoad];
    self.collectionView.bounces = NO;  
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.pagingEnabled = YES;
    // 注册cell
    [self.collectionView registerClass:[XMGNewFeatureCell class] forCellWithReuseIdentifier:ID];
    [self setUpAllChildView];
}
#pragma mark - 添加所有子控件
- (void)setUpAllChildView
{
    // guide1
    UIImageView *guide = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guide1"]];
    _guideView = guide;
    guide.centetX = self.view.centetX;
    [self.collectionView addSubview:guide];
    // guideLine
    UIImageView *guideLine = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLine"]];
    guideLine.x -= 170;
    [self.collectionView addSubview:guideLine];
    // largerText
    UIImageView *largerText = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLargeText1"]];
    largerText.centetX = self.view.centetX;
    largerText.centetY = self.view.height * 0.7;
    _guideLargetView = largerText;
    [self.collectionView addSubview:largerText];
    // smallText
    UIImageView *smallText = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideSmallText1"]];
    _guideSmallView = smallText;
    smallText.centetX = self.view.centetX;
    smallText.centetY = self.view.height * 0.8;
    [self.collectionView addSubview:smallText];
}

满地打滚卖萌求赞,如果本文帮助到你,轻点下方的红心,给作者君增加更新的动力。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,198评论 6 514
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,334评论 3 398
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,643评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,495评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,502评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,156评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,743评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,659评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,200评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,282评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,424评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,107评论 5 349
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,789评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,264评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,390评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,798评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,435评论 2 359

推荐阅读更多精彩内容

  • 资料收集自网络... 1.通讯录• 学习目的:控制器跳转,控制器之间传值,数据存储2.项目演示:(4个界面,交给4...
    b485c88ab697阅读 311评论 0 0
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,064评论 3 38
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,117评论 4 61
  • 我们在上一篇《通过代码自定义不等高cell》中学习了tableView的相关知识,本文将在上文的基础上,利用sto...
    啊世ka阅读 1,512评论 2 7
  • 最近,总会浮现你的一切,有家庭,有孩子,有车有房有番狗,一切似乎很美满,而我呢?好像一朵蒲公英,一吹,就散了……我...
    桃树的眼泪阅读 114评论 2 0