iPhone UITableView(利用UITableView實現平滑的九宮格效果)

 

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

 

最終實現的效果如下:

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章