IOS学习笔记(三)TableVIew

 

若是能准确知道数组容量的时候,不要用array直接实例化可变数组,[NSMutableArray arrayWihCapacity:50];   <  可优化很多。ios

 

。若是有须要跟踪NSArray中的内容,须要重写descriptionWithLocal 方法。首先新建一个NSArray 的Category,在新建出来的.m文件中加入下列代码数组

 

- (NSString *)descriptionWithLocal:(id)local 缓存

{app

  NSMutableString *string = [NSMutableString string];ide

  [string appendString:@"("];优化

  for (id obj in self) {编码

    [string appendFormat:@"\n\t\"%@\",", obj];spa

  }代理

  [string appendString:@"\n)"];orm

  return string;

}

如此一来,用NSLog显示NSArry中的数据再也不是utf-8编码,显示为原来数据。

 

UITableView

须要一个数据源来显示数据


1.需加入数据源协议                                UITableViewDataSource     ;     <

2. 设置表格的组数                              - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView   ;      <      

3.设置每一个分组中的行数                       - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  ;    <

4.设置每一个分组中显示的数据内容               - (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPaht:(NSIndexPath *)indexPath  {

                    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIndentifier:nil];

                    indexPath.section      即为分组名     indexPath.row     即行名

}  cell便是返回的分组中的数据。  

  

  [cell.textLabel setText:    ];   <

  [cell.imageView setImage:     ];   <

  [cell.detailTextLabel setText:     ];   <    

每一个ViewCell中都有一个辅助指示视图 accessoryType   默认是UITableViewCellAccessoryNone  即不显示辅助指示视图  其余值以下

 经过 cell setAccessoryType:UITableViewCellAccessoryNone     ;  <   来设置

 

                          

5.设置分组的标题            - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSIntger)secton       ;    <

6.设置页脚的标题, 分组底部的子标题        - (NSString *)tableView:(UITableVIew *)tableView titleForFooterInSection:(NSInteger)section     ;    <

7.字典返回全部keys的方法         NSArray *items = [self.cities allKeys];    <   里面数据出来的顺序是乱的

8.将UITableView中的分组分开(即设置style,但style只能在初始化的时候设定)  

                    UITableView *tableView = [[UITableView alloc] initWithFrame: style:(UITableViewStyle)]         ;     <

9.代理方法:     选中某一行所进行的操做 - (void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndex *)indexPaht   ;   <

          右侧索引方法     - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView     ;    <

               设置行高      - (CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath   ;    <

10.刷新某一行的数据       [tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationLeft];  <

11.刷新所有数据            [tableView reloadData];

12.更新某一行数据         [_tableView reloadRowsAtIndexPaths:[_tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationLeft];

 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

UITableView的优化:(条件为在storyBoard中的tableView中添加一个tableViewCell, 并设置identifier为myCell)否则会报错!

  static NSString *cellIdentifier = @"myCell"; //设置统一的标示符  static 能够保证表格标识符永远只有一个

  UITableViewCell *cell = [tableView dequeueReusableCellWithIndentifier:cellIndentifier];   //首先在缓存池中找名为''myCell''的单元格对象

      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#(NSString *)#> forIndexPath:<#(NSIndexPath *)#>] //自定义单元格时使用  

  if (cell = nil) {                

   cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellstyleDefault  reuseIdentifier:cellIdentifier]; //若是没找到,实例化一个新的cell

}  ios中,当页面内的tableView能容纳10行,当向下滑动时,第一行的数据地址进入缓存池,新刷出来的第十一行将从缓存池取出原第一行的地址用于存储第十一行数据

 

若是使用了storyBoard,而且,在storyBoard中指定了"单元格"的可重用标识符,那么

dequeueReusableCellWithIdentifier

dequeueReusableCellWithIdentifier 

 

第一种状况:

  若是在viewDidLoad注册了nib文件 / 若是在viewDidLoad注册了自定义单元格的类,而且指定了''单元格''的可重用标识符,那么

  dequeueReusableCellWithIdentifier

  dequeueReusableCellWithIdentifier:forIndexPath        两个方法等效

第二种状况:  

  若是没有在viewDidLoad注册nib文件 / 没有在viewDidLoad注册自定义单元格的类,那么,只能使用

  dequeueReusableCellWithIdentifier           而且须要判断cell没有被实例化,并作相应的处理

 

 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

经过拖动控件的方式来建立TableViewControl时,需注意设置tableViewCell的identifier!

用segue进行View的切换    在prepareForSegue方法中可找到全部的segue,但不一样的segue的标识符不同

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {  

   CitiesViewController *controller = segue.destinationViewController ;

  [controller setCities:cities];      在CitiesViewController.h文件中定义了cities这一属性,并用这一属性构造了视图,此处只需用cities设置属性便可

}

---------------------------------------------------------------------------------------------------------------------------------------------------------

[tableView registerClass:[chatCell class] forCellReuseIdentifier:@"myCell"]    为表格注册自定义单元格的类,并指定单元格的可重用"标识符"

----------------

1.在storyBoard中直接自定义单元格会注册单元格原型

2.用xib方式自定义单元格须要注册NIB

UINib *nib = [UINib nibWithNibName:@"bookCell" bundle:[NSBundle mainBundle]];

[tableView registerNib:nib forCellReuseIdentifier:@"cell"];

3.用代码方式自定义单元格须要注册类

[tableView registerClass:[MyCell class] forCellReuseIdentifier:CellIdentifier];

如下方法的目的就是要求必须注册自定义单元格类

[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

 

 

 

 

 

 

 

 

 

 

细节决定成败!                     

相关文章
相关标签/搜索