`
dcj3sjt126com
  • 浏览: 1885440 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

UITableView 基本使用方法总结

    博客分类:
  • IOS
iOS 
阅读更多

 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. {  
  4.     return 1;  
  5. }  

 

 

 

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

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

        

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

                还是勾勾标记。  

 

               

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

 

 

 

 

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

               返回指定的 section 的header的高度

 

                

  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就不起作用了。

 

                

  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

                

  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 才行。

 

 

              

  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. {  
  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;

              

  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. {  
  3.     return UITableViewCellEditingStyleDelete;  
  4. }  

 

 

 

              -(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;

分享到:
评论

相关推荐

    iOS-使用表格视图UITableView

    一、UITableView基本结构 UITableView主要由两部分构成:UITableView对象和UITableViewCell对象。UITableView是容器,负责显示和管理所有的单元格(cells),而UITableViewCell则是列表中的每一行。 二、创建...

    uitableview

    本代码示例主要探讨了 `UITableView` 的基本使用方法,以及如何与 `UINavigationController` 配合实现导航功能,特别是逆向传值。 首先,我们来详细了解一下 `UITableView`。`UITableView` 是 iOS 中的一种视图控件...

    UIScrollView/UITableView布局的影响因素总结

    10. **动态高度(Dynamic Height)**:如果单元格的内容高度不固定,可以使用`tableView:heightForRowAtIndexPath:`方法返回每个单元格的高度。 11. **性能优化**:大量数据可能导致性能问题,因此应合理使用`...

    iphone UITableView详解 带实例 例子

    本文详细介绍了 UITableView 的基本使用方法,并提供了完整的示例代码。通过这些内容,开发者可以更好地理解和使用 UITableView,构建出更丰富的列表界面。希望本文能帮助开发者解决 UITableView 使用过程中的常见...

    UITableView表格的修改

    总结,UITableView的修改涉及数据源的更新、代理方法的实现以及各种编辑操作的处理。熟练掌握这些知识点,可以构建出功能丰富的表格视图,为用户提供直观且高效的交互体验。通过实际项目中的不断实践和调试,你将...

    UITableView 下拉刷新EGOTableViewPullRefresh

    总结来说,`UITableView`的下拉刷新功能通过`EGOTableViewPullRefresh`库变得简单易实现。这个库提供了一个灵活的框架,不仅可以快速添加基本的下拉刷新,还可以根据需求进行定制。通过理解并运用上述步骤,开发者...

    UITableView ios风格控件.rar

    总结,UITableView是iOS应用中不可或缺的组件,它提供了展示大量数据的高效方式。通过理解并熟练掌握UITableView的数据源、委托、单元格自定义以及性能优化,开发者能够构建出功能丰富的交互式列表界面。在实际项目...

    iOS中UITableView使用的常见问题总结

    设置section数和行数是UITableView的基本配置,需要遵循UITableViewDataSource协议: ```swift extension MyViewController: UITableViewDataSource { func numberOfSections(in tableView: UITableView) -> Int {...

    UITableView详解

    总结起来,UITableView的使用涵盖了数据管理、用户交互、界面定制等多个方面。理解并熟练掌握这些知识点,对于iOS开发者来说至关重要,能帮助你构建出高效且易用的数据展示界面。在实际项目中,你可能还会遇到更多...

    iOS UI基础 UITableView 团购代码

    通过学习和实践这个例子,开发者不仅可以掌握UITableView的基本用法,还能进一步了解如何利用XIB进行界面设计,以及如何处理用户交互和性能优化。这些技能对于iOS开发来说至关重要,尤其在构建复杂和动态的数据驱动...

    UITableView实现时间轴效果

    总结,实现UITableView的时间轴效果涉及自定义单元格、数据模型的设计、 UITableViewDataSource和UITableViewDelegate的实现,以及可能的动画和布局优化。通过这些步骤,开发者可以创建出直观、易用的时间轴界面,为...

    UITableViewDemo

    UITableViewDemo 是一个示例项目,主要展示了如何在iOS应用中使用UITableView进行数据展示,同时结合SQLite3数据库进行数据管理,包括增、删、查等基本操作。在这个项目中,开发者采取了良好的代码组织策略,将...

    iOS开发UI篇—UITableview控件简单介绍.docx编程资料

    为了有效地使用`UITableView`,开发者需要理解其数据源的原理,并能够正确地实现`UITableViewDataSource`协议中的方法。此外,还需要掌握如何根据不同的需求选择合适的样式,以及如何通过代码或Storyboard来配置这些...

    iOS开发之UITableView与UISearchController实现搜索及上拉加载,下拉刷新实例代码

    总结来说,这个实例展示了如何在iOS应用中集成基本的表格功能、搜索功能以及刷新加载机制。通过理解这些代码,开发者可以构建出更复杂的用户界面,提供更丰富的交互体验。同时,了解如何利用代理和协议来协调不同...

    使用UItableview在iOS应用开发中实现好友列表功能

    本篇将详细讲解如何使用UITableView来实现一个好友列表功能,这里我们将以Objective-C语言为例进行说明。 一、项目结构和plist文件 首先,为了实现好友列表,我们需要准备一个基本的项目结构。在这个例子中,我们...

    iOS 下拉刷新

    总结来说,iOS的下拉刷新功能主要涉及UIRefreshControl的使用,以及可能涉及到的通知机制。通过学习这个Demo,开发者能够快速掌握下拉刷新的实现,并根据自己的需求进行定制和扩展。在实际项目中,理解并灵活运用...

    关于table view的总结

    ### 关于Table View的总结 #### 一、Table View简介 Table View是iOS应用中非常常见的一种用户界面组件,主要用于展示一系列数据项。...通过这个通讯录项目实例,初学者可以更好地理解和掌握Table View的使用方法。

    tableView的使用详解-ios移动开发

    本教程将深入探讨`UITableView`的使用,包括其基本概念、初始化、数据源与代理、自定义单元格以及性能优化。 首先,我们要理解`UITableView`的基本结构。`UITableView`是由行(`UITableViewCell`)组成的视图,每行...

Global site tag (gtag.js) - Google Analytics