1
//UITableView组头和组尾(header/footer)
// 返回组头标题:在Group样式下,标题的英文内容会自动变大写(Plain样式不会变大写)
// 组头一般都是标题性质的,较短,都会自动换行
- (NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section
{
XMGGroup *g = self.groups[section];
return g.headerStr;
}
// 返回组尾文字
// 组尾一般都是说明性质的,较长,都会自动换行
- (NSString )tableView:(UITableView )tableView titleForFooterInSection:(NSInteger)section
{
XMGGroup *g = self.groups[section];
return g.footerStr;
}
// 修改组头.组尾的高度(Plain样式下生效): 在Group样式下,组头组尾高度都是系统默认的
self.tableView.sectionHeaderHeight = 2;
self.tableView.sectionFooterHeight = 222;
2
// 设置tableView的表头表尾视图
// 一般用于做下拉刷新,banner
self.tableView.tableHeaderView = [UIButton buttonWithType:UIButtonTypeContactAdd];
self.tableView.tableFooterView = [[UISwitch alloc] init];
3.
//分割线
// 设置分割线颜色:
// [UIColor clearColor]如果设置成透明色就表示分割线取消显示
warning 在tableView上,尽量避免使用透明色
self.tableView.separatorColor = [UIColor orangeColor];
// UITableViewCellSeparatorStyleNone 取消分割线的显示
warning UITableViewCellSeparatorStyleNone->UITableViewCellSelectionStyleNone
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
4.
//cell的一些设置的优先级
(1)//设置cell的背景视图,并且backgroundView的优先级比backgroundColor高一些
cell.backgroundColor = (row % 2)? [UIColor yellowColor]: [UIColor orangeColor];
UIView *cellBg = [[UIView alloc] init];
cellBg.backgroundColor = [UIColor grayColor];
cell.backgroundView = cellBg;
(2)//cell的辅助视图,作用优先级高于accessoryType
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.accessoryView = [[UISwitch alloc] init];
(3)//3种样式下,selectedBackgroundView他的优先级高于selectionStyle
// 在UITableViewCellSelectionStyleNone样式中,设置selectedBackgroundView是无效的
UIView *cellsBg = [[UIView alloc] init];
cellsBg.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = cellsBg;
5
//cell的辅助视图样式 cell.accessoryType =
UITableViewCellAccessoryNone, // don’t show any
UITableViewCellAccessoryDisclosureIndicator, // 尖尖 regular
UITableViewCellAccessoryDetailDisclosureButton, // 圈i + 尖尖info
UITableViewCellAccessoryCheckmark, // 小对勾 checkmark.
UITableViewCellAccessoryDetailButton //圈i info
6
//cell的选中样式 cell.selectionStyle =
UITableViewCellSelectionStyleNone, // 设置没有选中样式
UITableViewCellSelectionStyleBlue, // 在iOS6之前,选中背景色变蓝,iOS7之后,和gray相同
UITableViewCellSelectionStyleGray, // 默认样式
UITableViewCellSelectionStyleDefault NS_ENUM_AVAILABLE_IOS(7_0) // 默认样式
7
//cell上面的子控件要添加到cell.contentView上
8
//UITableView 常见代理/易错属性
前两者易错
// 选中某一行时候调用
- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
// 取消选中某一行时候调用
- (void)tableView:(UITableView )tableView didDeselectRowAtIndexPath:(NSIndexPath )indexPath
// 根据indexPath返回对应的高度
- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
//在设置分割线样式为None的时候如果无效,先看你是否搞混了下列2个枚举
UITableViewCellSeparatorStyleNone//分割线样式
UITableViewCellSelectionStyleNone//选中样式
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
9
//cell的性能优化
思考:cell进入屏幕创建,离开屏幕销毁;频繁调用极耗性能如何解决?
利用tableView的缓存池,存放离开屏幕的cell
当cell进入屏幕后,先取缓存池若没有再新建
这样就可以只创建n+1个cell来展示所有数据
n表示屏幕上显示cell的最大数量
cell的循环利用
确定重用标识
static修饰局部变量是改变了生存期
static修饰全局变量主要是修改了作用域
根据标识从缓存池中取
封装若没有根据注册创建,拿到return,nil继续
nil后继续查看storyboard中是否有带重用标识的cell
若返回nil则手动创建
覆盖cell上的数据
返回cell
方法1:传统
方法2:注册
方法3:storyboard
10
// 注册
warning 面试问,registerClass 是iOS6出现(伴随UICollectionView) registerNib 是iOS5出现的, 注册的系统自带的cell,都是UITableViewCellStyleDefault类型的cell, 所以注册一般用于自定义cell的时候
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:xxxID];
11
//理解注册的工作原理 – 苹果内部的dequeueReusableCellWithIdentifier方法实现
- (UITableViewCell )testdequeueReusableCellWithIdentifier:(NSString )ID {
UITableViewCell *cell;
// 1.第一步,从缓存池中取cell
// cell = [xxx cellWithID:ID];
if (cell) { // 如果缓存池中有cell,直接返回,方法执行完毕
return cell;
}
// 2.如果此时没有cell,会继续执行
// 根据注册的信息来自动创建相应的cell
//cell = [cell类型[(注册的cell类型) alloc] initwithStyle:UITableViewCellStyleDefault(注册的cell都是default样式) ID:ID]
if (cell)
{
return cell;
}
// 3.如果此时还继续执行的话,就再看一眼storyBoard中是否有带ID重用标示的cell木有
// cell = storyBoard中是否有带ID重用标示的cell;
if (cell) {
return cell;
}
return nil;
}
12
// 返回索引条的方法
- (NSArray )sectionIndexTitlesForTableView:(UITableView )tableView
{
// 遍历组模型数组,拿到每个模型的title属性
// NSMutableArray *arrayM = [NSMutableArray array];
// for (XMGGroup *g in self.groups) {
// [arrayM addObject:g.title];
// }
// 通过KVC映射一个数组
return [self.groups valueForKeyPath:@”title”];
}
13
//索引条的颜色和字体颜色的设置
// 设置索引条的背景颜色
self.tableView.sectionIndexBackgroundColor = [UIColor yellowColor];
// 设置索引条的字体颜色
self.tableView.sectionIndexColor = [UIColor redColor];
14
// 由于是KVC是调用了所有的set方法,所以dict->Model可以抽取到set方法
- (void)setCars:(NSArray *)cars
{
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dict in cars) {
// 将每个dictCar转成模型
XMGCar *car = [XMGCar modelWithDict:dict];
[arrayM addObject:car];
}
_cars = arrayM;
}
15
//封装cell的另一种做法(注册)
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
// 0.确定重用标示(规范的写法)
static NSString *ID = nil;
if (ID == nil) {
ID = [NSString stringWithFormat:@”%@ID”, NSStringFromClass(self)];
}
// 如果没有注册过,就注册1次
static BOOL isReged = NO;
if (isReged == NO) {
[tableView registerClass:self forCellReuseIdentifier:ID];
isReged = YES;
NSLog(@"%s, line = %d", __FUNCTION__,__LINE__);
}
// 1.缓存池
// 这里应该是XMGCarCell *cell,写成id 的原因是为了拖个代码块
id cell = [tableView dequeueReusableCellWithIdentifier:ID];
return cell;
}