zoukankan      html  css  js  c++  java
  • UItableView一些小方法


    分类: UITableView和UIScrollView

    目录(?)[+]

    1、UItableView设置偏移量

    通过设置tableView的偏移量,让列表默认滚动到某个位置,内涵段子里面的效果

    1. [myTableView setContentOffset:CGPointMake(0, 100) animated:YES];  

    2、刷新某行cell的方法

    有时候只需要刷新某行的cell的数据,完全没必要调用[tableView reloadData]刷新整个列表的数据,调用以下方法即可。。
    1. NSIndexPath *indexPath_1=[NSIndexPath indexPathForRow:1 inSection:0];  
    2.       NSArray *indexArray=[NSArray  arrayWithObject:indexPath_1];  
    3.       [myTableView  reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationAutomatic];  

    3、改变分组列表之间的间距

     方法一

    1. 改变每一组之间的间距  
    2.  self.tableView.sectionHeaderHeight = 5;  
    3.  self.tableView.sectionFooterHeight = 0;  
    方法二
    1. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section  
    2. {  
    3.     return 5;  
    4. }  
    5.    
    6. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section  
    7. {  
    8.     return 5;  
    9. }  
    10.    
    11. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
    12. {  
    13.     UIView *view = [[UIView alloc] initWithFrame:CGRectZero];  
    14.     return view;  
    15. }  
    16.    
    17. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section  
    18. {  
    19.     UIView *view = [[UIView alloc] initWithFrame:CGRectZero];  
    20.     return view;  
    21. }  

    4、iOS7上tableView的分割线左边间距为0

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

    5、设置tableview的cell颜色

    1. //方法一:  
    2. cell.contentView.backgroundColor = [UIColor redColor];  
    3. //方法二:  
    4. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
    5. UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];  
    6. bgview.opaque = YES;  
    7. bgview.backgroundColor = [UIColor orangeColor];  
    8. [cell setBackgroundView:bgview];  
    9. //方法三:  
    10. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath  
    11. {  
    12. cell.backgroundColor = [UIColor redColor];   
    13. }  

    6、调整cell之间的距离

    1. #pragma mark 重写setFrame方法,自己调整cell的frame  
    2. - (void)setFrame:(CGRect)frame  
    3. {  
    4.     // tableView边框的宽度 #define kTableBorderWidth 5  
    5.     // 更改x、宽度  
    6.     frame.origin.x = kTableBorderWidth;  
    7.     frame.size.width -= kTableBorderWidth * 2;  
    8.       
    9.     // 更改顶部间距、每个cell之间的间距  
    10.     frame.origin.y += kTableTopBorderWidth;  
    11.     frame.size.height -= kTableViewCellMargin;  
    12.       
    13.     [super setFrame:frame];  
    14. }  

    7、cell选中和不选中显示不通背景

      1. cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"choose_item_selected.jpg"]];  
      2. cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"choose_item.jpg"]]; 

    8、设置 tableHeader 据表格顶部间距

    m_tableView.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);

        m_tableView.scrollIndicatorInsets = UIEdgeInsetsMake(44, 0, 0, 0);

  • 相关阅读:
    类型“System.Windows.Markup.IUriContext”在未被引用的程序集中定义 解决办法
    c# 根据文件流查看文件真实格式
    WPF ListBoxItem 使用Command命令添加双击事件
    MVVM 在使用 ItemsSource 之前,项集合必须为空
    WPF 自定义TextBox,可控制键盘输入内容
    百万数据如何在前端快速流畅显示?
    NodeJS npm 包装包失败的解决方案
    node.js express 4.x 安装指南(Express不是内部或外部命令解决方案)
    IIS8 不能在此路径中使用此配置节。如果在父级别上锁定了该节
    Npoi操作excel
  • 原文地址:https://www.cnblogs.com/allanliu/p/4826808.html
Copyright © 2011-2022 走看看