代码改变世界

UITableView的简单使用

2011-09-17 16:01  Paul Wong  阅读(11562)  评论(3编辑  收藏  举报

UITableView简介:

  有关于UITableView的介绍,在苹果官网上有比较具体的介绍,下面我还是简单复述一下下。表用于显示数据列表,数据列表中的每项都由行表示 ,共主要作用如下:

  • 为了让用户能通过分层的数据进行导航
  • 为了把项以索引列表的形式展示
  • 用于分类不同的项并展示其详细信息
  • 为了展示选项的可选列表

UITableView的种类:

  • 一种是分组表:                                                          另一种是无格式表 :

1.2                                                     1.1

UITableView的组成:

  表中的每一行都由一个UITableViewCell表示,可以使用一个图像、一些文本、一个可选的辅助图标来配置每个UITableViewCell对象,其模型如下:

cell

UITableViewCell类为每个Cell定义了一些属性:

  • textLabel:Cell的主文本标签(一个UILabel对象)
  • detailTextLabel:Cell的二级文本标签,当需要添加额外细节时(一个UILabel对象)
  • imageView:一个用来装载图片的图片视图(一个UIImageView对象)

UITableView的创建:

create

1.视图布局:UITableView类继承自UIScrollView类,像其他视图一样,其实例通过窗体定义自己的边界,还可以是  其他视图的子类或父类。UITableViewController负责处理布局,并会使用一个UITableView进行填充。

2.指定数据源:UITableView实例依赖外部资源按需为新表格单元或现有表格单元提供内容,数据源根据索引路径提 供表格单元格,索引路径是NSIndexPath类的对象,描述通过数据树到达特定节点的路径,即它们的分段和它们的行。

myIndexPath=[NSIndexPath indexPathForRow:5 inSection:0];

3.指定委托:UITableView实例使用委托响应用户交互,并实现有意义的响应,委托告知表格将响应这些交互的责任移交给指定对象,委托必须实现UITableViewDelegate协议。

 

实践:

有了上面的一些基本知识,下面做一个小实验。

效果图:

phone1                                        iphone2

 

实现的一些关键代码:

  RootViewController是主屏幕控制器,用来展示最上方的示图。示图的每一行分别由不同的类来创建。

 其每行创建代码如下:

   1:  - (void)createRows
   2:  {
   3:      [self addSectionAtIndex:0 withAnimation:UITableViewRowAnimationFade];
   4:      
   5:          [self
   6:              appendRowToSection:0
   7:              cellClass:[LabelCell class]
   8:              cellData:@"This is row"           
   9:              withAnimation:
  10:           UITableViewRowAnimationLeft];
  11:      
  12:   
  13:      [self addSectionAtIndex:1 withAnimation:UITableViewRowAnimationFade];
  14:      
  15:          [self
  16:              appendRowToSection:1
  17:              cellClass:[NibLoadedCell class]
  18:              cellData:@"This is row" 
  19:              withAnimation:
  20:           UITableViewRowAnimationLeft];
  21:              
  22:      
  23:   
  24:      [self addSectionAtIndex:2 withAnimation:UITableViewRowAnimationFade];
  25:      
  26:          [self
  27:              appendRowToSection:2
  28:              cellClass:[TextFieldCell class]
  29:              cellData:
  30:                  [NSMutableDictionary dictionaryWithObjectsAndKeys:
  31:                      @"TextField",
  32:                          @"label",
  33:                      @"", @"value",
  34:                      NSLocalizedString(@"input value here", @""),
  35:                          @"placeholder",
  36:                  nil]
  37:           withAnimation:UITableViewRowAnimationLeft];
  38:      [self hideLoadingIndicator];
  39:  }

 

编写UITableViewCell子类的代码:

   1:  + (NSString *)nibName
   2:  {
   3:      return @"NibCell";
   4:  }
   5:   
   6:  - (void)handleSelectionInTableView:(UITableView *)aTableView
   7:  {
   8:      [super handleSelectionInTableView:aTableView];
   9:       
  10:      NSInteger rowIndex = [self indexPath].row;
  11:      [((PageViewController *)aTableView.delegate).navigationController
  12:          pushViewController:
  13:              [[[DetailViewController alloc] initWithRowIndex:rowIndex] autorelease]
  14:          animated:YES];
  15:  }
  16:   
  17:  - (void)configureForData:(id)dataObject
  18:      tableView:(UITableView *)aTableView
  19:      indexPath:(NSIndexPath *)anIndexPath
  20:  {
  21:      [super configureForData:dataObject tableView:aTableView indexPath:anIndexPath];
  22:       
  23:      label.text = dataObject;
  24:  }
 
以上内容是本人学习ios开发的一个小笔记,代码借鉴Matt Gallagher的作品,根据其作品作详细的学习,并加以修改。谢谢阅读,希望对您有用。
完整代码下载TableDesign