UITableView 基本使用方法总结

本文介绍 UITableView 的基本使用方法,包括实现 UITableViewDelegate 和 UITableViewDataSource 协议,设置代理,实现数据源和代理方法等。涵盖设置表格视图的行数、高度、单元格内容及响应用户交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://blog.youkuaiyun.com/tangaowen/article/details/6438362

 

UITableView  基本使用方法

 

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

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

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

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

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

        

  1. //返回有多少个Sections 
  2. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  3.     return 1; 

 

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

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

       

  1. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  2.     return 10; 

 

         (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 可以设置是饱满的向右的蓝色箭头,还是单薄的向右箭头,

                还是勾勾标记。 

              

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2.     static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell"
  3.     UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier]; 
  4.     if (cell == nil) 
  5.     { 
  6.         // Create a cell to display an ingredient. 
  7.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  
  8.                                        reuseIdentifier:showUserInfoCellIdentifier]  
  9.                 autorelease]; 
  10.     } 
  11.      
  12.     // Configure the cell. 
  13.     cell.textLabel.text=@"签名"
  14.     cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str()  encoding:NSUTF8StringEncoding]; 
  15.         } 
  16.          

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

               返回指定的 section 的header的高度

               

  1. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
  2.     if (section ==0) 
  3.         return 80.0f; 
  4.     else 
  5.         return 30.0f; 

 

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

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

               

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

 

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

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

               

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

 

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

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

              

  1. TableView.allowsSelection=YES; 

 

              

  1. cell.selectionStyle=UITableViewCellSelectionStyleBlue; 

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

              

  1. TableView.allowsSelection=NO; 

 

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

              

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

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

              cell.selectionStyle = UITableViewCellSelectionStyleNone;

             

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

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

              

  1. self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine; 

 

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

 

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

              

  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 可以被编辑

               首先要进入编辑模式:

              

  1. [TableView setEditing:YES animated:YES]; 

 

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

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

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

              

  1. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
  2.     return UITableViewCellEditingStyleDelete; 

 

              -(void) tableView:(UITableView *)aTableView

commitEditingStyle:(UITableViewCellEditingStyle) editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

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

             

  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;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值