UITableView 基本使用方法总结

来源:互联网 发布:执法记录仪删除数据 编辑:程序博客网 时间:2024/04/28 06:26

                                                       UITableView  基本使用方法

 

 

  1.   首先,Controller需要实现两个  delegate ,分别是  UITableViewDelegate 和  UITableViewDataSource

 

   2.然后 UITableView对象的 delegate要设置为 self。

 

   3. 然后就可以实现这些delegate的一些方法拉。

 

       (1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;   

         这个方法返回 tableview 有多少个section 

        

[cpp] view plaincopy
  1. //返回有多少个Sections  
  2. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView   
  3. {  
  4.     return 1;  
  5. }  
 

         (2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;

        这个方法返回   对应的section有多少个元素,也就是多少行。

       

[cpp] view plaincopy
  1. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section   
  2. {  
  3.     return 10;  
  4. }  
 

 

         (3)- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;

                  这个方法返回指定的 row 的高度。

                - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

                  这个方法返回指定的 section的header view 的高度。

                - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

                  这个方法返回指定的 section的footer view 的高度。

 

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

                返回指定的row 的cell。这个地方是比较关键的地方,一般在这个地方来定制各种个性化的 cell元素。这里只是使用最简单最基本

                的cell 类型。其中有一个主标题 cell.textLabel 还有一个副标题cell.detailTextLabel,  还有一个 image在最前头 叫 

                cell.imageView.  还可以设置右边的图标,通过cell.accessoryType 可以设置是饱满的向右的蓝色箭头,还是单薄的向右箭头,

                还是勾勾标记。  

 

               

[cpp] view plaincopy
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   
  2. {  
  3.     static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell";  //语句0
  4.     UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];  //语句1
  5. /*
  6. 这里有必要解释一下:
  7. 可以这样理解,cell有一个地方(假设称为队列),专门存放那些生成过的,但是后来由于滚动tableView而隐藏起来的cell,而代码中语句1就是从队列中根据标示符取出一个暂时不用的cell,只有cell为nil,也就是队列中没有旧的cell的时候,才会执行语句2,生成一个新的cell。如果有旧的,就不用执行语句2了,这样节省资源,算作一种重用吧。在tableView初始化的时候队列中肯定没有cell的,所以每个cell生成的时候都会执行一遍2,当屏幕显示满了,向上滚动显示下一行时,就会把第一行隐藏,放到那个队列中,然后新增加的一行执行语句1的时候,结果就不是nil了,然后,就跳过语句2了,这样就节约资源了。
  8. 当然,上面这样对于使用系统提供的cell格式是没有什么问题,但是如果自己在cell上添加一些控件时,比如一个label,有时就会出现问题。尤其是各个cell的label的文字不相同时。首先这个添加的过程一定要在语句2之后,这样才是一次添加,如果放在语句3之后,那么由于cell的重用可能旧的上面已经有label了,你再添加一个,造成多次添加。其次label的文本值必须保证每次都要重新设置,也就是在语句3之后设置,这样才能保证每次必须执行。如果放在语句2后面,那么当使用旧的cell时,仍会保留旧的label文字,这是不对的。所以是在2后添加,在3后设置,可以在2添加的时候设一个tag值,这样可以在3处通过tag值获取控件设置。

    关于语句0,如果每个cell的结构完全相同,那没问题,就用这一个标识符,但是如果各个cell结构不完全相同,有的有textfield,有的有button,有的有switch,那就不能互相重用了,只能每行用不同的标识符了,可以方便的利用(@"CMainCell%d", indexPath.row),保证不会重复,多组的再加上组号。那这还有必要用语句2吗?还是需要的,因为当自己滚动隐藏,下次再显示出来的时候,还是可以重用的。标识符完全可以每次用一个新的,但为了最大限度的重用性,节省资源,才想了这么多办法。

    总之,注意添加控件的位置,注意设置控件的位置,注意cell标识符。
  9.  */
  10.     if (cell == nil)  
  11.     {  
  12.         // Create a cell to display an ingredient.  
  13.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle   
  14.                                        reuseIdentifier:showUserInfoCellIdentifier]   //语句2
  15.                 autorelease];  
  16.     }  
  17.       
  18.     // Configure the cell.  
  19.     cell.textLabel.text=@"签名";  //语句3
  20.     cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str()  encoding:NSUTF8StringEncoding];  
  21.         }  
  22.           

             (5)- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

               返回指定的 section 的header的高度

[cpp] view plaincopy
  1. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section  
  2. {  
  3.     if (section ==0)  
  4.         return 80.0f;  
  5.     else  
  6.         return 30.0f;  
  7. }  

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

               返回指定的section 的 header  的 title,如果这个section header  有返回view,那么title就不起作用了。

 

[cpp] view plaincopy
  1. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  
  2. {  
  3.     if (tableView == tableView_)  
  4.     {  
  5.         if (section == 0)   
  6.         {  
  7.             return @"title 1";  
  8.         }   
  9.         else if (section == 1)   
  10.         {  
  11.             return @"title 2";  
  12.         }   
  13.         else   
  14.         {  
  15.             return nil;  
  16.         }  
  17.     }   
  18.     else   
  19.     {  
  20.         return nil;  
  21.     }  
  22. }  
 

 

              (7) - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

                  返回指定的 section header 的view,如果没有,这个函数可以不返回view

                

[cpp] view plaincopy
  1. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
  2. {  
  3.     if (section == 0)   
  4.     {  
  5.           
  6.         UIView* header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView"   
  7.                                                         owner: self  
  8.                                                       options: nil] lastObject];  
  9.        
  10.         else  
  11.         {  
  12.            return nil;  
  13.         }  
  14. }  

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

 

               当用户选中某个行的cell的时候,回调用这个。但是首先,必须设置tableview的一个属性为可以select 才行。     

[cpp] view plaincopy
  1. TableView.allowsSelection=YES;  
 

[cpp] view plaincopy
  1. cell.selectionStyle=UITableViewCellSelectionStyleBlue;  
 

              如果不希望响应select,那么就可以用下面的代码设置属性:

              

[cpp] view plaincopy
  1. TableView.allowsSelection=NO;  

 

              下面是响应select 点击函数,根据哪个section,哪个row 自己做出响应就好啦。

              

[cpp] view plaincopy
  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath   
  2. {  
  3.     if (indexPath.section == 1)   
  4.     {  
  5.         return;  
  6.     }  
  7.     else if(indexPath.section==0)  
  8.     {  
  9.         switch (indexPath.row)   
  10.         {  
  11.             //聊天  
  12.             case 0:  
  13.             {  
  14.                 [self  onTalkToFriendBtn];  
  15.             }  
  16.                 break;  
  17.                   
  18.             default:  
  19.                 break;  
  20.         }  
  21.     }  
  22.     else   
  23.     {  
  24.         return ;  
  25.     }  
  26.       
  27. }  

              如何让cell 能够响应 select,但是选中后的颜色又不发生改变呢,那么就设置 

              cell.selectionStyle = UITableViewCellSelectionStyleNone;

      

[cpp] view plaincopy
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     //cell被选中后的颜色不变  
  4.     cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  5. }  
 

            (9)如何设置tableview  每行之间的 分割线

              

[cpp] view plaincopy
  1. self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;  
 

               如果不需要分割线,那么就设置属性为 UITableViewCellSeparatorStyleNone  即可。

      (10)如何设置 tableview cell的背景颜色

              

[cpp] view plaincopy
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.         //设置背景颜色  
  4.         cell.contentView.backgroundColor=[UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1];  
  5. }  
 

 

             (11) - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

 

               这个函数响应,用户点击cell 右边的 箭头(如果有的话)

 

            (12)如何设置tableview 可以被编辑

               首先要进入编辑模式:

              

[cpp] view plaincopy
  1. [TableView setEditing:YES animated:YES];  
 

               如果要退出编辑模式,肯定就是设置为NO

 

                - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

                返回当前cell  要执行的是哪种编辑,下面的代码是 

              

[cpp] view plaincopy
  1. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     return UITableViewCellEditingStyleDelete;  //返回 删除  模式
  4.       // return UITableViewCellEditingStyleNone;  //返回 一个不为任何指定行提供插入新行和删除行 模式
  5.      
  6. }  

              -(void) tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle) editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

              通知告诉用户编辑了 哪个cell,对应上面的代码,我们在这个函数里面执行删除cell的操作。

 

[cpp] view plaincopy
  1. -(void) tableView:(UITableView *)aTableView  
  2. commitEditingStyle:(UITableViewCellEditingStyle) editingStyle  
  3. forRowAtIndexPath:(NSIndexPath *)indexPath  
  4. {  
  5.         [chatArray  removeObjectAtIndex:indexPath.row];  
  6.     [chatTableView  reloadData];  
  7. }  

             (13)如何获得 某一行的CELL对象

 

               - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;



0 0
原创粉丝点击