引言

iOS开发中,要实现表格数据展示,做常用的做法就是使用UITableView。

iOS小技能:UITableView的简介_iOS

I UITableView的简介

1.1 什么是UITableView?

  1. UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳。
  2. UITableView的两种样式:UITableViewStylePlain、UITableViewStyleGrouped。
  3. UITableView 的常用属性
  • 分隔线
//分隔线

[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];

/**

32位真彩位:ARGB(Alpha、red、green、blue)

24位真彩位:RGB

*/

[self.tableView setSeparatorColor:[UIColor colorWithRed:1.0 green:1.0 blue:0 alpha:1.0]];

//setTableHeaderView视图通常用于放置图片轮播器内容,与分组无关

UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];

[headerView setBackgroundColor:[UIColor redColor]] ;

[self.tableView setTableHeaderView:headerView];

//setTableFooterView视图通常做上拉刷新功能

UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];

[footerView setBackgroundColor:[UIColor redColor]];

[self.tableView setTableFooterView:footerView];

1.2 UITableView如何展示数据(代理模式)

1、UITableView需要一个数据源来显示数据 2、UITableView会向数据源查询:一共有多少行数据、每一行显示什么数据 3、没有设置datasource的UITableView,只是一个空壳 4、凡是遵守UITableViewDatasource协议的oc对象,都可以是UITableView的datasource 5、tableView &datasource的关系

6、tableView展示数据的过程:

//1.调用数据源的下面方法得知一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
//2.调用数据源的下面方法得知每一组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
//3.调用数据源的下面方法得知每一行显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
  • 代理模式


如果想为对象的某些方法做方法逻辑之外的附属功能(例如 打印出入参、处理异常、校验权限),但是又不想(或是无法)将这些功能的代码写到原有方法中,那么可以使用代理模式。


II 字典转模型举例

2.1 嵌套字典数组的模型转化

#import "HSCarGroup.h"




@implementation HSCarGroup

- (instancetype)initWithDictionary:(NSDictionary *)dict{

//KVC

self = [super init];//初始化父类属性

if (self) {

//初始化自身属性--将字典转化为HSCarGroup数据模型

[self setValue:dict[@"title"] forKey:@"title"];

//dict[@"cars"] 存放的是字段数组,与解析plist文件之后得到的数组类型一致;因此可以将dict[@"cars"]字典数组转化成HSCar模型数组

[self setCars:[HSCar cars:dict[@"cars"]]];

}

return self;

}

+ (instancetype)carGroupWithDictionary:(NSDictionary *)dict{

return [[self alloc]initWithDictionary:dict];

}

+ (NSArray *)carGroups{

NSMutableArray *tmpArrayM = [NSMutableArray array];

//解析plist

NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total" ofType:@"plist"];

NSArray *arrayDict = [NSArray arrayWithContentsOfFile:path];

for (NSDictionary *dict in arrayDict) {

[tmpArrayM addObject:[self carGroupWithDictionary:dict]];
}
return tmpArrayM;
}

- (NSString *) description{

return [NSString stringWithFormat:@"<%@: %p> {title: %@, cars: %@}",self.class,self,self.title,self.cars];

}
@end

2.2. KVC 之数组取值

(常常应用于tableVie的索引数组生成)

// *****右侧索引列表*********
/**
返回的索引数组的“元素内容”和分组无关;索引数组的下表对应分组的下标
*/
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
//1.使用遍历方式获取索引数组
// NSMutableArray<NSString *> *indexTitleArrayM = [NSMutableArray array];
// for (HSCarGroup *CarGroup in self.carGroups) {
// [indexTitleArrayM addObject:CarGroup.title];
// }
// return indexTitleArrayM;

//2.KVC(键值编码) cocoa的大招,用于间接修改、获取对象的属性的一种方式(字典转模型、过滤数组获取数据)

/**

1>使用kvc在获取数据时,如果取值对象没有包含keypath的“键名”,会自动进入对象的内部查找

2》若取值的对象是一个数组,则取值结果也是一个数组

*/

// NSLog(@"%@",[self.carGroups valueForKeyPath:@""]);

return [self.carGroups valueForKeyPath:@"title"];
}

III MVC(model、view、controller)

3.1 MVC的特性

1》View上面显示什么内容,取决于model 2》只要model数据改了,View的显示状态会跟着变更 3》controller负责初始化model,并将model数据传递给view去解析展示

3.2 MVC特性的应用

  • (tableView的行移动、添加、删除)

/**

支持手势拖拽删除。删除事件的处理需要自己处理-》MVC,只要修改model,view显示的状态内容也会跟着改变

1.编辑风格类型:

typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {

UITableViewCellEditingStyleNone,0

UITableViewCellEditingStyleDelete,1

UITableViewCellEditingStyleInsert2

};

2.表格格控件的修改操作实现过程,记得注重边界测试



*/

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

//1.处理删除键的监听事件

if ( UITableViewCellEditingStyleDelete == editingStyle) {

//1>删除处理,操作datasource

[self.dataList removeObjectAtIndex:indexPath.row];

//2>刷新表格

//*(重新加载所有数据)

// [self.tableView reloadData];

//*deleteRowsAtIndexPaths 表格控件动画地删除指定的行

[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

}else if (UITableViewCellEditingStyleInsert == editingStyle){

//2.插入数据处理

[self.dataList insertObject:@"new lydia" atIndex:indexPath.row+1];

NSIndexPath *idxPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];

//表格控件动画地在指定的indexPath数组添加指定的行

[self.tableView insertRowsAtIndexPaths:@[idxPath] withRowAnimation:UITableViewRowAnimationMiddle];





}



}


/**

移动表格的行

*/


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

//根据sourceIndexPath、destinationIndexPath调整model数据

// [self.dataList exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];

//从数组取出源

id sourceObject = self.dataList[sourceIndexPath.row];

//从数组删除源

[self.dataList removeObjectAtIndex:sourceIndexPath.row];

NSLog(@"%@",self.dataList);




//将源插入数组的目标位置

[self.dataList insertObject:sourceObject atIndex:destinationIndexPath.row];

NSLog(@"%@",self.dataList);

}

#pragma mark - UITableViewDelegate代理方法

/**

If not implemented, all editable cells will have UITableViewCellEditingStyleDelete set for them when the table has editing property set to YES.

*/

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

return (indexPath.row % 2)?UITableViewCellEditingStyleDelete :UITableViewCellEditingStyleInsert;

}

表格的修改,常常用于本地数据缓存的场景,例如:个人通讯录、QQ聊天记录、本地日记本

see also

???? 联系作者: iOS逆向(公号:iosrev)


 ​


???? 简历模板、技术互助。关注我,都给你。