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来用
在开发中我们可能因为自己的疏忽或者其他原因 弄错了
会显示以下报错
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)
- ** xib自定义cell **
表格中的数据操作
- 创建一个可变数组来保存数据
@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];
}
满地打滚卖萌求赞,如果本文帮助到你,轻点下方的红心,给作者君增加更新的动力。