UI之UITableView的全部属性、方法以及代理方法执行顺序,常规的设置技巧

一,属性

1.UITableview基本属性

   frame-------------设置控件的位置和大小
   backgroundColor--------设置控件的颜色
   style--------获取表视图的样式
   dataSource---------设置UITableViewDataSource的代理
   delegate---------设置UITableViewDelegate代理
   sectionHeaderHeight------设置组表视图的头标签高度
   sectionFooterHeight--------设置级表视图的尾标签高度
   backgroundView----------设置背景视图,只能写入
   editing----------是否允许编辑,默认是NO
   allowsSelection----------在非编辑下,行是否可以选中,默认为YES
   allowsSelectionDuringEditing----------控制某一行时,是否可以编辑,默认为NO
   allowsMultipleSelection--------是否可以选择多行,默认为NO
   allowsMutableSelectionDuringEditing----------在选择多行的情况下,是否可以编辑,默认为NO
   sectionIndexMinimumDisplayRowCount-------------显示某个组索引列表在右边当行数达到这个值,默认是NSInteger的最大值
   sectionIndexColor------------选择某个部分的某行改变这一行上文本的颜色
   sectionIndexTrackingBackgroundColor--------设置选中某个部分的背景颜色
   separatorStyle----------设置单元格分隔线的样式
   separatorColor---------设置选中单元格分隔线的颜色
   tableHeaderView---------设置组表的头标签视图
   tableFooterView----------设置组表的尾标签视图

2.UITableView类目属性

   section--------获取当前在哪个组内
   row------------获取当前单元格是第几行

二,方法

1.UITableview的方法:

   初始化方法
   initWithFrame:-----------设置表的大小和位置

   initWithFrame:style---------设置表的大小,位置和样式(组,单一)

   setEditing:----------表格进入编辑状态,无动画
   setEditing: animated:---------表格进入编辑状态,有动画
   reloadData---------------刷新整个表视图
   reloadSectionIndexTitles--------刷新索引栏
   numberOfSections-----------获取当前所有的组
   numberOfRowsInSection:---------获取某个组有多少行
   rectForSection:----------获取某个组的位置和大小
   rectForHeaderInSection:---------获取某个组的头标签的位置和大小
   rectForFooterInSection:-----------获取某个组的尾标签的位置和大小
   rectForRowAtIndex:-----------获取某一行的位置和大小
   indexPathForRowAtPoint-------------点击某一个点,判断是在哪一行上的信息。
   indexPathForCell:------------获取单元格的信息
   indexPathsForRowsInRect:---------在某个区域里会返回多个单元格信息
   cellForRowAtIndexPath:-------------通过单元格路径得到单元格
   visibleCells-----------返回所有可见的单元格
   indexPathsForVisibleRows--------返回所有可见行的路径
   headerViewForSection:--------设置头标签的视图
   footerViewForSection;----------设置尾标签的视图
   scrollToRowAtIndexPath:(NSIndexPath *)indexPathatScrollPosition:scrollPosition animated:(BOOL)animited-----------滑到指定行的位置,可以配置动画 
   beginUpdates--------只添加或删除才会更新行数
   endUpdates---------添加或删除后会调用添加或删除方法时才会更新
   insertSections:withRowAnimation:-----------插入一个或多个组,并使用动画
   insertRowsIndexPaths:withRowAnimation:-------插入一个或多个单元格,并使用动画
   deleteSections:withRowAnimation:--------删除一个或多个组,并使用动画
  deleteRowIndexPaths:withRowAnimation:--------删除一个或多个单元格,并使用动画
  reloadSections:withRowAnimation:---------更新一个或多个组,并使用动画
  reloadRowIndexPaths:withRowAnimation:-------------更新一个或多个单元格,并使用动画
  moveSection:toSection:-------------移动某个组到目标组位置
  moveRowAtIndexPath:toIndexPath:-----------移动个某个单元格到目标单元格位置
  indexPathsForSelectedRow----------返回选择的一个单元格的路径
  indexPathsForSelectedRows---------返回选择的所有的单元格的路径
  selectRowAtIndexPath:animation:scrollPosition---------设置选中某个区域内的单元格 
  deselectRowAtIndexPath:animation:----------取消选中的单元格
  重用机制

  dequeueReusableCellWithIdentifier:---------获取重用队列里的单元格

2.UITableViewDataSource代理方法

   方法:
   numberOfSectionsInTableView:------------设置表格的组数
   tableView:numberOfRowInSection:----------设置每个组有多少行
   tableView:cellForRowAtIndexPath:---------设置单元格显示的内容
   tableView:titleForHeaderInSection:---------设置组表的头标签视图
   tableView:titleForFooterInSection:-----------设置组表的尾标签视图
   tableView:canEditRowAtIndexPath:---------设置单元格是否可以编辑
   tableView:canMoveRowAtIndexPath:--------设置单元格是否可以移动
   tableView:sectionIndexTitleForTableView:atIndex:-------设置指定组的表的头标签文本
   tableView:commitEditingStyle:forRowAtIndexPath:----------编辑单元格(添加,删除)
   tableView:moveRowAtIndexPath:toIndexPath-------单元格移动

3.UITableViewDelegate代理方法

   tableView:  willDisplayCell: forRowAtIndexPath:-----------设置当前的单元格
   tableView: heightForRowAtIndexPath:-----------设置每行的高度
   tableView:tableView heightForHeaderInSection:-----------设置组表的头标签高度
   tableView:tableView heightForFooterInSection:-------------设置组表的尾标签高度
   tableView: viewForHeaderInSection:----------自定义组表的头标签视图
   tableView: viewForFooterInSection: ----------自定义组表的尾标签视图
   tableView: accessoryButtonTappedForRowWithIndexPath:-----------设置某个单元格上的右指向按钮的响应方法
   tableView: willSelectRowAtIndexPath:-----------获取将要选择的单元格的路径
   tableView: didSelectRowAtIndexPath:-----------获取选中的单元格的响应事件
   tableView: tableView willDeselectRowAtIndexPath:------------获取将要未选中的单元格的路径
   tableView: didDeselectRowAtIndexPath:-----------获取未选中的单元格响应事件 

三,方法执行顺序

   执行顺序如下
   第一轮
   1、numberOfSectionsInTableView    :假如section=2,此函数只执行一次,假如section=0,下面函数不执行,默认为1
   2、heightForHeaderInSection  ,执行两次,此函数执行次数为section数目
   3、heightForFooterInSection  ,函数属性同上,执行两次
   4、numberOfRowsInSection    ,此方法执行一次
   5、heightForHeaderInSection     ,此方法执行了两次,我其实有点困惑为什么这里还要调用这个方法
   6、heightForFooterInSection   ,此方法执行两次,
   7、numberOfRowsInSection,执行一次
   8、heightForRowAtIndexPath  ,行高,先执行section=0,对应的row次数
   第二轮
   1、numberOfSectionsInTableView ,一次
   2、heightForHeaderInSection  ,section次数
   3、heightForFooterInSection    ,section次数  
   4、numberOfRowsInSection    ,一次
   5、heightForHeaderInSection  ,执行section次数
   6、heightForFooterInSection,执行section次数
   7、numberOfRowsInSection,执行一次
   8、heightForRowAtIndexPath,行高,先执行一次
   9、cellForRowAtIndexPath  
  10、willDisplayCell
  然后8、9、10依次执行直到所有的cell被描画完毕

四,刷新表格

     整体刷新
1> * reloadData 整体刷新(每一行都会刷新)
     局部刷新
1>局部section刷新
  * NSIndexSet *nd = [[NSIndexSet alloc] initWithIndex:1]; //刷新第二个section
  * [self.tableView reloadSections:nd withRowAnimation:UITableViewRowAnimationAutomatic];
2>局部cell刷新
  * NSIndexPath *te=[NSIndexPath indexPathForRow:2 inSection:0];//刷新第一个section的第二行
  * [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:te,nil] withRowAnimation:UITableViewRowAnimationMiddle];

五,UITableViewCell的选中时的颜色设置

1>系统给出的默认设置
   *无色  
  cell.selectionStyle = UITableViewCellSelectionStyleNone;  
   *蓝色  
  cell.selectionStyle = UITableViewCellSelectionStyleBlue;  
   *灰色  
  cell.selectionStyle = UITableViewCellSelectionStyleGray;
2>自定义颜色和背景设置
   *颜色
    改变UITableViewCell选中时背景色
    UIColor *color = [[UIColoralloc]initWithRed:0.0green:0.0blue:0.0alpha:1];//通过RGB来定义自己的颜色
    cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:cell.frame] autorelease];  
    cell.selectedBackgroundView.backgroundColor = [UIColor  xxxxxx];  
   *背景图片
    cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell_backGround.png"]];   

六.添加阴影效果

   1>添加四个边阴影

       示例:
       iamgView.layer.shadowColor  =  [UIColor blackColor].CGColor;
       iamgView.layer.shadowOffset = CGSizeMake(0,4)
       iamgView.layer.shadowOpacity = 0.5;
       iamgView.layer.shadowRadius = 10.0;

   2>添加两个边阴影

       示例:
       imgvPhoto.layer.shadowColor = [UIColor blackColor].CGColor;//阴影的颜色
       imgvPhoto.layer.shadowOffset = CGSizeMake(4, 4);//偏移距离
       imgvPhoto.layer.shadowOpacity = 0.5;//不透明度
       imgvPhoto.layer.shadowRadius = 2.0;//半径

    3>给cell添加阴影效果

       cell.layer.shadowOffset = CGSizeMake(0, 1);
       cell.layer.shadowColor = [UIColor grayColor].CGColor;
       cell.layer.shadowRadius = 1;
       cell.layer.shadowOpacity = .5f;
       CGRect shadowFrame = cell.layer.bounds;
       CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:shadowFrame].CGPath;
       cell.layer.shadowPath = shadowPath;

七. 添加圆角

       iamgView.layer.cornerRadius = 10;
       iamgeView.layer.masksToBounds = YES;
       注意:不要给section footer 或 section header设置背景色,否则无效果。

八.添加边框   

       iamgView.layer.borderColor=[UIColor blackColor].CGColor;
       iamgView.layer.borderWidth=0.3;

九. 设置tableviewcell不让选中 

      1:cell.userInteractionEnabled = NO;
      2:[cell setSelectionStyle:UITableViewCellSelectionStyleNone];   

    注意:这两种方法都能实现不让cell有点击事件。

        但第一个是关闭cell的交互,这样会使其不能相应事件;如果cell中有button第一个方法也会让button失去点击事件。
        第二种方法不会影响交互. 

十.设置缩进级别

-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
 if (indexPath.row ==0) {
        return10;
    }
  return 0;
}

十一.点击后,过段时间cell自动取消选中

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //消除cell选择痕迹
    [self performSelector:@selector(deselect) withObject:nil afterDelay:0.5f];
}
-(void)deselect{
    [self.tableview deselectRowAtIndexPath:[self.tableview indexPathForSelectedRow] animated:YES];
}

十二.监听tableView滑到顶部,或者底部

由于TableViewUISCrollView的子类,所可以在其代理方法中进行监听
 1>获取tableView的偏移量
NSInteger  offset  = self.commentTableView.contentOffset.y;
 2>判断滑到底部
        CGFloat height =scrollView.frame.size.height;
        CGFloat contentYoffset =scrollView.contentOffset.y;
        CGFloat distanceFromBottom =scrollView.contentSize.height-contentYoffset;
        if (distanceFromBottom < height) {
            NSInteger   distance =height -distanceFromBottom;
            if (self.commentTableView.contentOffset.y <AdaptedHIGHT(891)) {
               [self.commentTableView setContentOffset:CGPointMake(0, offset + distance/3)];
            }
       }
 3>判断滑到顶部
   if(scrollView.contentOffset.y <0){
            if(self.commentTableView.contentOffset.y>0){
                [self.commentTableView setContentOffset:CGPointMake(0, offset+scrollView.contentOffset.y/3)];
            }
     }
示例:
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
       if (scrollView==self.mvTableView) {
          NSInteger  offset  = self.commentTableView.contentOffset.y;
          CGFloat height =scrollView.frame.size.height;
          CGFloat contentYoffset =scrollView.contentOffset.y;
          CGFloat distanceFromBottom =scrollView.contentSize.height-contentYoffset;
          if (distanceFromBottom < height) {
              NSInteger   distance =height -distanceFromBottom;
              if (self.commentTableView.contentOffset.y <AdaptedHIGHT(891)) {
                [self.commentTableView setContentOffset:CGPointMake(0, offset + distance/3)];
             }
           }        
         if(scrollView.contentOffset.y <0){
             if(self.commentTableView.contentOffset.y>0){
                [self.commentTableView setContentOffset:CGPointMake(0, offset+scrollView.contentOffset.y/3)];
            }
        }
    }
}

十三iOS,监听tableVIew的偏移量

   1. 添加监听

   [self.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNewcontext:nil];

   2.监听的处理

   - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
            if ([keyPath isEqualToString:@"contentOffset"]){
             CGPoint offset = [change[NSKeyValueChangeNewKey] CGPointValue];
        }
    }

   3.移除监听

    [self.tableView removeObserver:self forKeyPath:@"contentOffset" context:nil];

十四,UItableView设置偏移量

通过设置tableView的偏移量,让列表默认滚动到某个位置

[self.tableView  setContentOffset:CGPointMake(0, 100) animated:YES];  

十五,iOS7上tableView的分割线左边间距为0

ios7里面tableViewCell上面的分割线,左边少了20个像素,用下面的方法,可以让分割线完整显示出来
if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {  
           [_tableView setSeparatorInset:UIEdgeInsetsZero];  
  }  


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值