zoukankan      html  css  js  c++  java
  • UITableView

      1 UITableView内置了两种样式:UITableViewStylePlain,UITableViewStyleGrouped
      2 
      3 <UITableViewDataSource,UITableViewDelegate>里的方法:
      4 tableView处理步骤
      5 #pragma mark 1.有多少组
      6 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
      7 #pragma mark 2.第section组头部控件有多高
      8 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
      9 #pragma mark 3.第section组有多少行
     10 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     11 #pragma mark 4.indexPath这行的cell有多高
     12 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
     13 #pragma mark 5.indexPath这行的cell长什么样子
     14 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     15 #pragma mark 6.第section组头部显示什么控件
     16 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
     17 
     18 
     19 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
     20 
     21 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     22 
     23 //每当有一个cell进入视野屏幕就会调用,所以在这个方法内部就需要优化。
     24 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     25 if(cell==nil){
     26      //在这里面做创建的工作。循环优化。防止刷新cell进入屏幕的时候重复的创建
     27 } 
     28 }
     29 
     30 //当调用reloadData的时候,会重新刷新调用数据源内所有方法,其他事情都不会做呀
     31  [self reloadData]
     32 
     33  //这个方法只有在一开始有多少条数据才会算多少个高度,这个方法只会调用一次,但是每次reloadData的时候也会调用
     34  //而且会一次性算出所有cell的高度,比如有100条数据,一次性调用100次
     35 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
     36 
     37 
     38 
     39 
     40 -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView //右侧索引
     41 
     42     -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath //行点击事件
     43 
     44 NSIndexPath *path = [self.tableView indexPathForSelectedRow]; //获得被选中的indexPath可以得到section,row
     45   
     46 [self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationNone]; //刷新table指定行的数据
     47          
     48    [self.tableView reloadData]; //刷新table所有行的数据
     49 
     50 
     51 UITableView常用属性:
     52     UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain]; // 初始化表格
     53     分隔线属性 
     54 tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; //UITableViewCellSeparatorStyleNone;
     55 [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; //取消分隔线
     56     tableView.separatorColor = [UIColor lightGrayColor];
     57     
     58     条目多选
     59 tableView.allowsMultipleSelection = YES;
     60      
     61     // 设置标题行高
     62 [_tableView setSectionHeaderHeight:kHeaderHeight];
     63     [_tableView setSectionFooterHeight:0];
     64 
     65     // 设置表格行高
     66  [_tableView setRowHeight:50];
     67 
     68 //设置背景色
     69 self.tableView.backgroundView  优先级高,如果要设置backgroundColor的时候要先把view设置为nil
     70 self.tableView.backgroundColor
     71 
     72 //在tableView的头部或者尾部添加view,footerView宽度是不用设置的
     73        xxxView.bounds = CGRectMake(0,0,0,height); 
     74     self.tableView.tableFooterView =xxxView;
     75        self.tableView.tableHeaderView =xxxView;
     76 
     77 UIButton *bt = (UIButton*)[self.contentView viewWithTag:i+100];
     78  
     79 增加tableview滚动区域
     80 self.tableView.contentInset = UIEdgeInsetsMake(0, 0, xx, 0);  
     81 UITableViewCell
     82 //创建UITableViewCell         
     83 UITableViewCell *cell = [[UITableViewCell alloc]
     84 initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
     85         
     86     [cell.textLabel setBackgroundColor:[UIColor clearColor]];// 清空标签背景颜色 
     87         
     88         cell.backgroundView =xx; //设置背景图片
     89         cell.backgroundVColor =xx;
     90         cell.selectedBackgroundView = selectedBgView; //设置选中时的背景颜色
     91 
     92        
     93    cell.accessoryView = xxxView; //设置右边视图
     94    [cell setAccessoryType:UITableViewCellAccessoryNone]; //设置右侧箭头    
     95  
     96 [self setSelectionStyle:UITableViewCellSelectionStyleNone]; //选中样式
     97 cell.selectionStyle = UITableViewCellSelectionStyleBlue;
     98 
     99 //设置cell的高度
    100 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    101 
    102 contentView下默认有3个子视图,其中的2个是UILabel,通过textLabel和detailTextLabel属性访问,第3个是UIImageView,通过imageView属性访问.
    103   UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle
    104   
    105 #pragma mark - 重新调整UITalbleViewCell中的控件布局
    106 - (void)layoutSubviews{ 
    107 [super layoutSubviews];
    108 109 } 
    110 cell 里面还有一个contentView
    111 UITableViewCell表格优化
    112 UITableViewCell对象的重用原理:
    113 重用原理:当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免创建新对象
    114 还有一个非常重要的问题:有时候需要自定义UITableViewCell(用一个子类继承UITableViewCell),而且每一行用的不一定是同一种UITableViewCell(如短信聊天布局),所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的UITableViewCell,时可能会得到错误类型的UITableViewCell那么UITableView在重用UITableViewCell。解决方案:UITableViewCell有个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化一个UITableViewCell对象
    115 
    116 /**
    117 单元格优化
    118  1. 标示符统一,使用static的目的可以保证表格标示符永远只有一个
    119  2. 首先在缓冲池中找名为"myCell"的单元格对象
    120  3. 如果没有找到,实例化一个新的cell
    121  **/
    122 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    123     static NSString *cellIdentifier = @"myCell";
    124 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    125 //使用这种方法不用判断下面的cell
    126     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    127     if (cell == nil) {        
    128         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    129     }
    130 return cell;
    131 }
    132 
    133 表格的编辑模式
    134 删除、插入 
    135 - (void)setEditing:(BOOL)editing animated:(BOOL)animated;  开启表格编辑状态 
    136 
    137 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    138     返回表格编辑编辑样式。不实现默认都是删除
    139 return editingStyle : UITableViewCellEditingStyleDelete, UITableViewCellEditingStyleInsert
    140 }
    141 
    142 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    143       //根据editingStyle处理是删除还是添加操作
    144       完成删除、插入操作刷新表格
    145 - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
    146 
    147 -(void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
    148 }
    149 
    150 移动
    151 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
    152 sourceIndexPath 移动的行    
    153 destinationIndexPath 目标的行
    154 
    155 自定义表格行UITableViewCell
    156 storyboard方式创建:
    157 直接拖到UITableView里面设置UITableViewCell
    158 注意:
    159 1.通过XIB或者Storyboard自定义单元格时,在xib和Storyboard里面需要指定单元格的可重用标示符Identifier
    160 
    161 2.注意表格的优化中的差别
    162 在Storyboard中两者等效
    163 xxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    164 xxCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    165 
    166 在xib文件中有差别:
    167 第一种情况,只能在iOS 6以上使用,如果在viewDidLoad注册了nib文件,并且指定了“单元格”的可重用标示符,那么     
    168      dequeueReusableCellWithIdentifier:
    169      dequeueReusableCellWithIdentifier:forIndexPath:
    170      方法是等效的。如果在viewDidLoad中注册了nib文件,表格缓冲池中的管理,有系统接管!
    171 
    172 第二种情况,是在iOS 4以上均可以使用,如果没有在viewDidLoad注册nib文件,那么,只能使用
    173      dequeueReusableCellWithIdentifier:并且需要判断cell没有被实例化,并做相应的处理
    174 
    175   
    176 在代码创建中差别:
    177 用代码创建cell中的处理和nib一样,注册了cell就有系统接管并且可以用带forIndexPath的方法,没有注册就要自己去实例化cell,不能用带forIndexPath的方法
    178 [tableView registerClass:XxxCell class] forCellReuseIdentifier:@"xxCell"];
    179 
    180 xib方式创建:
    181 //注册Identifier 
    182 - (void)viewDidLoad{
    183     [super viewDidLoad];
    184     /**
    185      注意:以下几句注册XIB的代码,一定要在viewDidLoad中!
    186      注册XIB文件,获得根视图,并且转换成TableView,为tableView注册xib
    187      Identifier名要在xib文件中定义,并且保持一致
    188      **/
    189     UINib *nib = [UINib nibWithNibName:@"BookCell" bundle:[NSBundle mainBundle]];
    190     UITableView *tableView = (UITableView *)self.view;
    191     [tableView registerNib:nib forCellReuseIdentifier:@"bookCell"];    
    192 }
    193 
    194 //没有注册Identifier只能使用下面方法
    195 static NSString *CellIdentifier = @"bookCell";
    196 BookCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    197 if (cell == nil) { 
    198         cell = [[BookCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    199         NSBundle *bundle = [NSBundle mainBundle];
    200         NSArray *array = [bundle loadNibNamed:@"BookCell" owner:nil options:nil];
    201         cell = [array lastObject];
    202     }
    203 
    204 
    205 代码方式创建:
    206 建立UITableViewCell的类,继承UITableViewCell
    207 往cell里面加入view的时候注意点:
    208 //新建的组件放入contentView中
    209 [self.contentView addSubview:xxView]; 
    210 
    211 //设置图片拉伸属性stretch
    212 UIImage *normalImage = [UIImage imageNamed:@"xx.png"];
    213 normalImage = [normalImage stretchableImageWithLeftCapWidth:
    214 normalImage.size.width / 2 topCapHeight:normalImage.size.height / 2];
    215 
    216 //在tableView里面viewDiDLoad里面要注册cell类
    217 [tableView registerClass:XxxCell class] forCellReuseIdentifier:@"xxCell"];
    218 
    219 自定义表格中Header
    220 //自定义表格在这个方法中定义
    221 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    222 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  • 相关阅读:
    Go标准库Context
    事务并发处理: DB+ORM+逻辑代码
    日志:slf4j+log4j+maven配置
    Shiro workshop
    JSP Workshop
    sql records
    Java内存模型(JMM)
    Application, JDBC, 数据库连接池, Session, 数据库的关系
    Java位操作全面总结
    Effective Java总结
  • 原文地址:https://www.cnblogs.com/developer-wang/p/4520364.html
Copyright © 2011-2022 走看看