UITableView 基础

/* 


 

 

 5. 数据源方法的调用顺序

 5.1 获取有多少组

 5.2 根据对应组获取有多少行

 5.3 根据组和行的索引,获取cell

 

 5.2 加上返回行高的代理方法之后的调用顺序

 5.1 获取返回主

 5.2 返回行

 5.3 获取每一行的高度

 5.4 获取UITableViewCell

 5.5 获取行高

 5.6 行高和cell互相调用

 

 注意:1. 以后凡是可以滚动的界面,通常最终继承自UIScrollView

 2. UIScrollView本身不可以滚动,需要设置contentSize属性

 但是UITableView可以滚动,我们并没有设置contentSize

 

 

 调用顺序的原理

 1. 获取对应数据的组数

 2. 根据对应数据的具体组获取到对应的行数

 3. 第一次获取行高的时候,是去进行计算,tableViewcontentSize是多少

 4. 计算完上面的流程,界面可以滚动了,但是界面上展现哪些数据,这个时候还不确定

 5. 在获取一遍组数 行数 行高

 6. 开始计算

 6.1 得到屏幕的高度

 6.2 去计算每行cell的高度

 6.3 根据屏幕的高度 cell的高度 去计算一个界面上应该显示多少个cell

 (虽然英雄有97个数据,但它并不是一次性全部展现)

 7. 计算完界面上展现的cell之后,这个时候才开始真正的获取要展现的控件

 UITableViewCell方法

 8. 每展现一个UITableViewCell,一定要注意,先通过数据源方法得到cell控件

 然后再给cell控件设置frame

 9. 然后重复第8步流程—>界面上显示多少个cell就重复几次


 

 

 


  UITableView的两种样式

 1>.Plain普通样式

 

 2>.GRouped组样式

 

 区别

 组样式 1>.组与组之间有一块空白区域作为间隔,而普通样式没有

          2>.头标题会随着cell滚动消失

 

 普通样式 1>.组与组之间没有间隔

          2>.在屏幕最上方,组标题一直显示,直到下一个标题滚动到它的位置顶替它。

 

 

 常见的UITableViewDataSource中包含的方法

 

 dataSource的使用  UITableView使用这个控件数据源的时候记得 1.设置数据源 2.遵守数据源协议

 也可以在storyboard中右击连线设置,但是UITableViewcontroller不用设置这些,它内部已经设置

 

 常用的五个数据源方法

 

 1>.设置当前组内数据的行数

   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

 

 2>.设置当前行所展现的表格(数据)

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

 

 3>.设置有多少组

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

 

 4>.设置对应组的头标题

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section


 5>.设置对应组的尾标题

  - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

 

 

 

 什么是UITableViewCell

 一个继承了UIVIew的类,所以它也是一个控件,这个控件中包含了自己一些特有的子类

 几个常用的,里面还有好多子类

 

 1>.imageView 图片

 

 2>.textLabel  标题

 

 3>.detailTextLabel 详细描述标题

 

 4>.contentView  内部的View,如果想往tableView中添加子控件,需要用它来添加

 

 5>.设置右侧标签(配件,附件)图标

 //设置一个系统默认的样式图标

 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;


 6>. //设置一个自定义的View

 cell.accessoryView = view;

 

  7>.设置cell的背景view

 //设置cell的默认背景view

 cell.backgroundView

 

  8>.//设置cell的默认背景颜色,注意:这个方法是属于UIScrollView,当同时设置了colorview用户将看到view,因为view的图层在color上面

 cell.backgroundColor = [UIColor redColor];

 

 9>.设置cell被选中时的背景view

 //设置cell被选中时的背景view

 cell.selectedBackgroundView = view;

 

 

 cell几种常用样式

 

 1>.Default  最左边是   配图    右边            标题

 

 2>.Subtitle          配图    右边上面是  标题  标题下面是  详细描述

 

 3>.Value1           配图     右边  标题  在右边   详细描述


 4详细描述      没有配图    标题  在右边   详细描述

 

 


 

 行高  如果统一设置行高的话  self.tableView.rowHeight = 70;

 

 如果行高不一致时通过代理方法

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

 

 if (indexPath.row == 2)

 

 return 100;

 

 }else{

 

 return 50;

 }

 


 设置分割线

 //设置分隔线的样式

 self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

 self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

 

 //设置分隔线的颜色

 self.tableView.separatorColor = [UIColor redColor];

 

 //设置分割线的内边距(上下是不起作用的)

 //左边会影响头像于字的距离,及分割线的远近

 //右边只会影响屏幕右边与分割线的距离

 self.tableView.separatorInset = [UIEdgeInsetsMake(0, 50, 0, 20);

 

 设置tableView的头部view(通常用来展示广告或者做下拉刷新)

 设置tableView的尾部view(通常用来做加载更多功能)

 

 

 cell重用   为了避免重复的创建 释放 最好加个static ,将它强制放到静态区中

 static NSString *ID = @"heros";

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

 if(cell ==nil)

 {

 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

 }

 

 

 刷新数据

 //1.第一种刷新数据的方法

 [self.tableView reloadData];

 

 //2.第二种刷新数据的方法

 [self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationBottom];

 

 实现右滑删除按钮

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

 

 删除数据并刷新

 [self.tableView deleteRowsAtIndexPaths:NSIndexPaths  withRowAnimation:UITableViewRowAnimation];

 

 

 

 //是否隐藏状态栏

 - (BOOL)prefersStatusBarHidden{

 

 return YES;

 }


 

 

 //左边会影响头像于字的距离,及分割线的远近

 //右边只会影响屏幕右边与分割线的距离

 self.tableView.separatorInset = UIEdgeInsetsMake(0, 30, 0, 30);

 

 //头和尾不能共用

 //如果头用尾什么也不显示,如果尾用头只会显示一空空白区域

 //创建一个View 做为headerView  它只有高度起作用

 UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 100)];

 

 view.backgroundColor = [UIColor redColor];

 

 self.tableView.tableHeaderView = view;

 

 

 //创建一个View 作为footerView x 高起作用;

 //x设置数据的话会显示的View会往右移动,

 UIView *View1 = [[UIView alloc]initWithFrame:CGRectMake(0, 100, 0, 100)];

 

 View1.backgroundColor = [UIColor blueColor];

 

 self.tableView.tableFooterView = view;

 


 //代理方法


 //点击某一行会触发这个方  添加一个可以修改当前cell的小案例

 //添加一个UIAlertView

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

 

 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"修改英雄的名字" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"修改", nil];

 

 

 //    UIAlertViewStyleDefault = 0,

 //    UIAlertViewStyleSecureTextInput,  加密输入框 输入密码

 //    UIAlertViewStylePlainTextInput,  普通的

 //    UIAlertViewStyleLoginAndPasswordInput  登录+密码

 alertView.alertViewStyle = UIAlertViewStylePlainTextInput;

 

 //获取输入框弹框的索引  有的弹框有两个框

 UITextField *textField = [alertView textFieldAtIndex:0];

 

 //获取模型数据

 XLHeroModel *model = self.heroModel[indexPath.row];

 

 //给输入框赋值,这样输入框就能显示点击cell的文字了

 textField.text = model.name;

 

 //设置

 alertView.tag = indexPath.row;

 

 //显示

 [alertView show];

 

 }

 

 //弹框的实现思想:创建一个alertVie,设置样式,获取输入框的索引(有的弹框出来有两个框),获取模型对应行的内容,给textField的赋值,然后show一下,点击cell的内容就显示到弹框中了,

 //修改弹框的思想:是在alertVie的代理方法中,先判断按钮的索引,拿到textField的数据,获取模型数据,用弹框中的数据给模型赋值,最后刷新,可以全局刷新也可以刷新当前行(根据索引获取当前行,再刷新)

 

 //按钮的点击事件  代理方法

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

 

 //这个是but的索引,只有给按钮设置索引按钮才有效

 if (buttonIndex != 0) {

 

 //修改数据

 //获取textFile的数据

 NSString *changeStr = [alertView textFieldAtIndex:0].text;

 

 //修改数据 数据来源与plist  最后修改的是数组中的数据 索引怎么拿?在点击cell的方法中有索引,所以在上个方法中设置alertViewtag值等于行的索引就行

 XLHeroModel *model = self.heroModel[alertView.tag];

 

 model.name = changeStr;

 

 //刷新表格

 

 //全局刷新  他会刷新 屏幕有多少行 就会刷新多少行

 //        [self.tableView reloadData];

 

 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:alertView.tag inSection:0];

 [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];

 

 }

 

 

 }


 //删除cell

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

 

 //因为NSArray数组完成初始化后长度不可变化,不可删除,和增加

 NSMutableArray *arrM = self.heroModel;

 

 [arrM removeObjectAtIndex:indexPath.row];

 

 self.heroModel = arrM;

 

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

 

 }

 


 #pragma mark - 移动cell 的功能

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

 

 

 }

 - (IBAction)开启 {

 

 self.tableView.editing = YES;

 }

 

 - (IBAction)关闭:(id)sender {

 

 self.tableView.editing = NO;

 }


 

 

 UITableView中有个返回组标题的数据源方法

 

 - (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{

 

 

 //    return [self.carsArray valueForKey:@"title"];

 

 //修改对应索引名字

 return @[@"",@"",@"",@"",@"a",@"b",@"E",@"F"];

 

 }


 //修改组索引的方法

 //字体颜色

 self.tableView.sectionIndexColor = [UIColor redColor];

 //背景颜色

 self.tableView.sectionIndexBackgroundColor = [UIColor grayColor];

 //点击是整个背景的颜色会变成这个颜色,离开后变成原来的背景颜色

     self.tableView.sectionIndexTrackingBackgroundColor = [UIColor greenColor];

 

//获取imageView的最大x

 CGRectGetMaxX(self.imageView.frame)

 

//获取imageView的中间值

 CGRectGetMidY(self.imageView.frame)


 //移动cell 设置多个操作的方式,如滑动扣扣有置顶,删除,移动

- (NSArray <UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{


    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"呵呵" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

               NSLog(@"这是action1要做的事情");

    }];

    

    UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"哈哈" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

        

        NSLog(@"只是action2要做的事情");

    } ];


    action1.backgroundColor = [UIColor greenColor];


    return @[action1,action2];

}





//修改deleta为删除

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return @"删除";


}

 

 


UITableViewCellStyleDefault 测试zhi有这种转台下可以让cell显示的title中间显示并且单独改变一行的颜色




 

*/


转载于:https://my.oschina.net/lufeidexin/blog/660941

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值