iPhone UITableView(利用UITableView实现平滑的九宫格效果)

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 04:13 点击:

UITableView是一种“目录视图或叫表视图”(英文名字table view),这种表视图以列表的形式显示或编辑信息,它由一列、多行组成。用户可以通过垂直滚动的方式导航到一个表视图的任意行上,并可以自定义每一行数据的显示方式。

 

在创建表视图的时候,可以选择两种风格的表视图:UITableViewStylePlain或者UITableViewStyleGrouped,前者是按索引进行排序的,而后者是按组进行分类显示的。

 

基本上每一个UITableView都有相应的UITableViewController、UITableViewDelegate、UITableViewDataSource类。UITableViewController类作为UITableView的视图控制类(MVC里Controller的角色)负责管理UITableView,它和大多数UIViewController类一样,控制着UITableView的生命周期函数。UITableViewDelegate作为委托模式里的被委托对象的接口,和大多数iPhone委托接口一样,它提供了UITableView子类无法在行为上保持一致的部分,在这里读者可以自定义表视图的显示风格,甚至可以自定义表视图的每一个元素,它的重要接口定义如下:

 

@protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>

@optional

 

// Display customization

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;

 

// Variable height support

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

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

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

 

// Section header & footer information. Views are preferred over title should you decide to provide both

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

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;  

 

// Accessories (disclosures).

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

 

// Selection

// Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;

 

// Called after the user changes the selection.

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

 

// Editing

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

 

// Indentation

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath; // return depth of row for hierarchies

 

@end

 

UITableViewDataSource提供了表视图的数据源,下表列出了常见的表视图数据源方法:

 

Method
 Description
 
tableView:numberOfRowsInSection:
 特定Section内的行数
 
numberOfSectionsInTableView:
 特定数据源的表视图的Section数目
 
tableView:cellForRowAtIndexPath:
 从数据源获取单元格内容并放到特定的行上
 
sectionIndexTitlesForTableView:
 获取一个数据源的表视图的标题
 
tableView:commitEditingStyle:forRowAtIndexPath
 提交单元格内容的修改
 
talbeView:canEditRowAtIndexPath:
 通过返回一个Boolean类型的值来通知表视图某一行能否修改
 
tableView:canMoveRowAtIndexPath:
 通过返回一个Boolean类型的值来通知表视图某一行能否被移动
 
tableView:moveRowAtIndexPath:toIndexPath:
 允许某一个表视图单元格被移动
 


 

表视图数据源接口提供了表视图数据源操作的常用方法,其中tableView:numberOfRowsInSection和tableView:cellForRowAtIndexPath:是每一个表试图的数据源必须实现的两个方法,前者告诉表视图内有多少行单元格,而后者告诉表视图每一个单元格的内容是什么。程序通过实现这两个方法,可以提供一个表视图所需要的基本信息并供表视图调用。

&nbs

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • Lexical or Preprocessor Issue 'xxx.h
      • ios学习笔记(二)xcode 4.3.2下实现基本交互
      • ios版本的helloworld
      • iphone(object-c) 内存管理(3) 有效的内存管理 前半部分
      • ios学习笔记(一)xcode 4.3.2下创建第一个ios项目
      • IOS类似iphone通讯录TableView的完整demo【附源码】
      • UITableView一些方法
      • [iPhone中级]iPhone团购信息客户端的开发 (二)
      • 如何为Iphone应用创建启动界面
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1