分享

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

 quasiceo 2014-12-01

 

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

 

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

 

基本上每一个UITableView都有相应的UITableViewControllerUITableViewDelegateUITableViewDataSource类。UITableViewController类作为UITableView的视图控制类(MVCController的角色)负责管理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:numberOfRowsInSectiontableView:cellForRowAtIndexPath:是每一个表试图的数据源必须实现的两个方法,前者告诉表视图内有多少行单元格,而后者告诉表视图每一个单元格的内容是什么。程序通过实现这两个方法,可以提供一个表视图所需要的基本信息并供表视图调用。

 

笔者在下面的例子里会编写一个带导航面板的表视图,这种复杂类型的控件在iPhone中随处可见,如iPod程序、备忘录程序、闹钟程序等。

 

ü         首先,新建一个“Window-based”项目并命名为“TableProjectOne”。

ü         新建一个UIViewController的子类,并命名为“MyViewController”。

ü         创建一个空的xib并命名为“MyViewController”。

ü         Interface Build中打开MyViewController.xib

ü         Library里拖拉一个viewMyViewController的窗口中,然后再添加一个table view到新添加的view下,确保table view完全填充view

ü         修改File’s OwnerMyViewController,连接MyViewControllerview到上面新添加的view上。

ü         到这里MyViewController创建完毕,保存并退出Interface Build

ü         打开MainWindow.xib并拖拉一个Navigation Controller到窗口中。

ü         拖拉一个View ControllerNavigation Controller下,改变它的类名和Nib名为MyViewController

ü         拖拉一个Bar Button ItemNavigation Controller上,并改title为“view2”。

 

修改TableProjectOneAppDelegate.h文件如下:

 

#import <UIKit/UIKit.h>

#import "MyViewController.h"

 

@interface TableProjectOneAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;

         IBOutlet UINavigationController *viewController;

}

 

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet UINavigationController *viewController;

 

@end

 

实现文件TableProjectOneAppDelegate.m如下:

 

#import "TableProjectOneAppDelegate.h"

 

@implementation TableProjectOneAppDelegate

 

@synthesize window;

@synthesize viewController;

 

- (void)applicationDidFinishLaunching:(UIApplication *)application {   

         [window addSubview:self.viewController.view];

    // Override point for customization after application launch

    [window makeKeyAndVisible];

}

 

- (void)dealloc {

    [window release];

         [viewController release];

    [super dealloc];

}

@end

 

最终实现的效果如下:

 

 

 


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多