UITableView 控件
1、拖一个控件UITableView
2、将控件的数据源代理设置为当前控制器
self.tableView.dataSource = self.
3.实现协议要求必须实现的两个方法
1)告诉控件一个分组有多少行
-(NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section
{
}
2)告诉控件每一个分组每一行显示什么内容 组信息管家:
indexPath
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
1.创建单元格
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
2.通过组信息管家获取当前组号,当前组内行号,用来设置每一个分组每一行显示什么内容
if (indexPath.section == 0) {
cell.textLabel.text = @"文章";
3.返回单元格
return cell;
}
4.实现协议的可选方法
1)告诉控件如要几个分组
-(NSInterger) numberOfSetSctionsInTableView:(UITableView *)tableView
{
return 2;
}
2)通过组号设置组头尾信息
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return @"出轨";
}else{
return @"吸毒";
}
}
当处理多组显示时 控件类 内部实现理解
组数由模型数组大小决定,由numberOfSetSctionsInTableView方法返回 (不实现默认返回1)
控件类会根据组数开始遍历生成组,遍历的过程中通过组调用numberOfRowsInSection(必须)方法获取行数,比如返回对象的一个数组属性变量的元素个数
由控件类同时将组数和该组行数存放组信息管家那里(NSIndexPath *)indexPath
当控件类需要给行添加具体数据内容时,调用 cellForRowAtIndexPath (必须)利用组信息管家获取组号,和行号
控件类由组号拿到数据对象,由该组的行号,拿到对象的一个数组属性变量的对应元素,开始塞数据。
明星列表思路归纳
分组显示不同类型的明星,用头描述类型,用尾描述原因
UI设计
1.拖入UITableView 控件 类型选为Grouped
代码设计
1.设置数据源dataSource
2.实现numberOfSectionsInTableView
3.实现tableView—numberOfRowsInSection
4.实现tableView—cellForRowAtIndexPath
5.实现tableView—titleForHeaderInSectiion
6.实现tableView—titleForFooterInSection
7.实现prefersStatusBarHidden
英雄LOL细节 cell重用 分析
注意:为空cell的个数:是屏幕能显示的个数+1;//刚启动时,显示全屏个,然后当滑动的时候会再生成一个
1.设置tableView的行高
self.tableView.rowHeight =40;
2.去找一个带有标识的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIndentifier:@“xxxx”];
3.创建一个带有标识的cell 注意样式:
UITableViewCellStyleSubtitle 只有这样才会显示详情子标题
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIndentifier:@“xxxx”];
4.cell 里面有
imageView;textLabel;detailTextLabel;backgroundView;accessoryType 属性
5.tableVie属性相关
1)self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
2) self.tableView.separatorColor = [UIColor blueColor];
3) self.tableView.separatorInset = UIEdgeInsetsMake(0,0,0,100);//只有左右有效果
4)self.tableView.tableHeaderView;self.tableView.tableHeaderView;是一个UIView类型的
代理方法
6.返回选中行的方法
tableView—willSelectRowAtIndexPath
7.即将取消选中的行
tableView—willDeselectRowAtIndexPath
8.已经选中
tableView—didSelectRowAtIndexPath
9.已经取消选中
tableView—didDeselectRowAtIndexPath
10.数据源方法
1)tableView—heightForRowAtIndexPath
2)sectionIndexTitlesForTableView//左侧索引栏
3)点击某个索引走到哪一组
tableView—sectionForSectionIndexTitle
11.KVC 用字典设置值
[self setValuesForKeysWithDictionary:dict];
当选中某个单元格的时候,弹窗,编辑
1)创建一个弹窗控制器
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@“修改”
message:nil
preferredStyle:UIAlertControllerStyleAlert];
2)添加文本框
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){}];
3)添加行为
UIAlertAction *cancel = [UIAlertAction
actionWithTitle:@“取消”
style:UIAlertActionStyleCancel
handler:nil];
[allertController addAction:cancel];
[self presentViewController:allerController animated:YES completion:nil];
关键:弹窗控制器,每添加一个UIAlertAction或UITextField控件,都添加到对应的数组里记录下来,方便下次再用
4)先修改数据源,在刷新表格
刷新全局:[self.tableView reloadData];
刷新局部:[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
设置单元格的编辑事件
1)self.tableView.editing = YES;
2) 当点击编辑按钮的时候会执行这个方法
tableView—commitEditingStyle—forRowAtIndexPath
利用第二个参数判断是删除,还是添加,
if(editingStyle == UITableViewCellEditingStyeDelete)
{
[self.heros removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[IndexPath] withRowAnimation:UITableViewRowAnimationRight];
}
else
{
HMHero *hero = self.heros[indexPath.row];
[self.heros insertObject:hero atIndex:0];
[self.tableView reloadData];
}
3)开启什么样的编辑模式
-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0)
{
return UITableViewCellEditingStyleInsert;
}
return UITableViewCellEditingStyleDelete;
}
复制,剪切,粘贴
1)扩展一个用来保存复制或剪切的对象属性
2)必须同时以下三个方法
1.是否要显示复制剪切粘贴菜单
-(BOOL)tableView:(UITableView *)tableView shoulShowMenuFroRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES:
}
-(BOOL)tableView:(UITableView *)tableView shouldShowMenuFroRowAtIndexPath:(NSIndexPath *)indexPath
-(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
-(BOOL)tableView:(UITableView *)tableView shouldSHowMenuForRowAtIndexPath:(NSindexPath *)indexPath
-(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
-(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
2.要不要执行某个操作
-(BOOL) tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
return YES:
}
3.当用户点击了按钮的时候需要做什么
-(void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
[self prformSelector:action withObect:indexPath];
//把方法转化为字符串:NSStringFromSelector(action)//action==copy: // cut:// paste:
}
-(void)copy:(NSIndexPath *)indexPath
{
self.hero = self.heros[indexPath.row];
}
-(void)cut:(NSIndexPath *)indexPath
{
self.hero = self.heros[indexPath.row];
[self.hero removeObjectAtIndex:indexPah.row];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
}
-(void) paste:(NSIndexPath *)indexPath
{
[self.heros insertObject:self.hero atIndex:indexPath.row];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}
1.存储代码块
1)选中点击2秒,再拖入代码块
2)删除代码块,先选中,然后按delete键
tableView样式
style
只是tableView显示样式,与tableView是否分组无关
UITableViewStylePlain
平铺样式
UITableViewStyleGrouped 当使用的时候head 和 foot 在滑动的的时候不再悬浮,而会跟着滑动
分组样式
注意:这个样式只能在创建tableView或storyboard设置,因为这个属性是只读属性,当使用
tableView的常用属性
可以用连线的方式设置数据源和代理
self.tableView.dataSource
self.tableView.delegate
设置行高
self.tableView.rowHeight = 60;
高度相关
rowHeight
行高
sectionHeaderHeight
分组头部高度
sectionFooterHeight
分组底部高度
设置分割线
separatorStyle
分割线样式
separatorColor
自定义tableView的头和尾部
tableHeaderView
头部视图
tableFooterView
尾部视图
设置国际化
1.点击项目project →Localizations 添加语言
2.点击target→options→language→Chinese
数据文件有数组里还有数组的情况
建两个数据模型
1)在从大模型向小模型出入次数组
2)这个次数组算大模型的一个数组属性
然分割线从左端开始
self.tableView.separatorInset = UIEdgeInsetsMake(0,0,0,10);