天天看點

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont

<b></b>

1.UITableView繼承UIScrollView.

<b>2.執行個體一城市清單思路:</b>

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html&gt;&lt;head&gt;&lt;meta http-equiv="Cont

步驟一:建立UITableView。UITableView樣式為組

步驟二:設定UITableView的資料源方法。

步驟三:實作UITableView的資料源方法,此方法會自動調用。

傳回有多少組傳回一組有多少行傳回每一行顯示的UITableViewCell(繼承UIView),initWithStyle使用這個方法調用。注意UITableView的資料源的方法普遍都是以tableView開頭。

步驟四:用數組管理資料。

步驟五:每個數組中都是一個字典,key:(header,footer,cityes).

工廠方法好處:簡化對象的執行個體化,快速建立對象。

UITableViewCellStyleDefault 

UITableViewCellStyleValue1

UITableViewCellStyleSubtitle

UITableViewCellStyleValue2

UITableViewCellAccessoryDisclosureIndicator

UITableViewCellAccessoryDetailDisclosureButton

UITableViewCellAccessoryCheckmark

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html&gt;&lt;head&gt;&lt;meta http-equiv="Cont

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"産品資訊展示" message:@"哈哈哈哈哈" delegate:nil cancelButtonTitle:@"ABC" otherButtonTitles:@"123",@"456", nil];

// 彈出UIAlertView

[alert show];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"産品資訊展示" message:@"哈哈哈哈哈" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;

alert.alertViewStyle = UIAlertViewStylePlainTextInput;

alert.alertViewStyle = UIAlertViewStyleSecureTextInput;

===================================================

步驟一:設定UIAlertView的代理

步驟二:遵守UIAlertView的協定

步驟三:實作UIAlertView的按鈕點選協定方法。

步驟一: 取出文本框文字 

注意UIAlertView中的文本框的角标是根據UIAlertView從上到下第幾個文本框決定的。最上面的文本框角标為0.

步驟二:  修改模型資料

步驟三:  重新整理表格

// 指定重新整理indexPaths數組裡的行數。

注意indexPaths裡存儲的是NSIndexPath對象

UITableView預設隻會加載出現在螢幕上面的cell,沒當有一個cell移除螢幕,就會存儲到緩存池裡找。

性能優化步驟:

步驟一:定義cell的辨別(不需要每次都建立cell辨別,是以需要使用static,static辨別隻會在第一次建立,以後都不會建立了。)

步驟二:從緩存池裡取cell

步驟三:判斷取出cell是否為空,如果為空就手動建立cell。

 ===================================================

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html&gt;&lt;head&gt;&lt;meta http-equiv="Cont

UITableView的資料源(dataSource)和代理(delegate)

UITableView需要一個資料源(dataSource)來顯示資料,UITableView會向資料源查詢一共有多少行資料以及每一行顯示什麼資料等。沒有設定資料源的UITableView隻是個空殼。凡是遵守UITableViewDataSource協定的OC對象,都可以是UITableView的資料源。

通常都要為UITableView設定代理對象(delegate),以便在UITableView觸發一下事件時做出相應的處理,比如選中了某一行。凡是遵守了UITableViewDelegate協定的OC對象,都可以是UITableView的代理對象。一般會讓控制器充當UITableView的dataSource和delegate

 —————————————————————————————————————

UITableViewDataSource

@required

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

第section分區一共有多少行

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

建立第section分區第row行的UITableViewCell對象(indexPath包含了section和row)

@optional

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

一共有多少個分區

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

第section分區的頭部标題

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

第section分區的底部标題

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

某一行是否可以編輯(删除)

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

某一行是否可以移動來進行重新排序

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;

UITableView右邊的索引欄的内容

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

選中了UITableView的某一行

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

某一行的高度

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

第section分區頭部的高度

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

第section分區尾部的高度

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

第section分區頭部顯示的視圖

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

第section分區尾部顯示的視圖

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath

設定每一行的等級縮進(數字越小,等級越高)

 ===============================================================

UITableViewCell

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html&gt;&lt;head&gt;&lt;meta http-equiv="Cont

UITableView的每一行都是一個UITableViewCell,通過dataSource的

tableView:cellForRowAtIndexPath:方法來初始化每一行

UITableViewCell是UIView的子類,内部有個預設的子視圖:contentView。contentView是UITableViewCell所顯示内容的父視圖,并負責顯示一些輔助訓示視圖。輔助訓示視圖的作用是顯示一個表示動作的圖示,可以通過設定UITableViewCell的accessoryType來顯示,預設是UITableViewCellAccessoryNone(不顯示輔助訓示視圖),其他值如下:

UITableViewCellAccessoryDisclosureIndicator 

--------------------------------------------------------------------------------------------------

UITableViewCell的contentView

contentView下預設有3個子視圖:其中的2個是UILabel(通過UITableViewCell的textLabel和detailTextLabel屬性通路),第3個是UIImageView(通過UITableViewCell的imageView屬性通路)

UITableViewCell還有一個UITableViewCellStyle屬性:用于決定使用contentView的哪些子視圖,以及這些子視圖在contentView中的位置

UITableViewCell對象的重用原理

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html&gt;&lt;head&gt;&lt;meta http-equiv="Cont

iOS裝置的記憶體有限,如果用UITableView顯示成千上萬條資料,就需要成千上萬個UITableViewCell對象的話,那将會耗盡iOS裝置的記憶體。要解決該問題,需要重用UITableViewCell對象

重用原理:當滾動清單時,部分UITableViewCell會移出視窗,UITableView會将視窗外的UITableViewCell放入一個對象池中,等待重用。當UITableView要求dataSource傳回UITableViewCell時,dataSource會先檢視這個對象池,如果池中有未使用的UITableViewCell,dataSource會用新的資料配置這個UITableViewCell,然後傳回給UITableView,重新顯示到視窗中,進而避免建立新對象

還有一個非常重要的問題:有時候需要自定義UITableViewCell(用一個子類繼承UITableViewCell),而且每一行用的不一定是同一種UITableViewCell(如短信聊天布局),是以一個UITableView可能擁有不同類型的UITableViewCell,對象池中也會有很多不同類型的UITableViewCell,那麼UITableView在重用UITableViewCell時可能會得到錯誤類型的UITableViewCell

解決方案:UITableViewCell有個NSString *reuseIdentifier屬性,可以在初始化UITableViewCell的時候傳入一個特定的字元串辨別來設定reuseIdentifier(一般用UITableViewCell的類名)。當UITableView要求dataSource傳回UITableViewCell時,先通過一個字元串辨別到對象池中查找對應類型的UITableViewCell對象,如果有,就重用,如果沒有,就傳入這個字元串辨別來初始化一個UITableViewCell對象

重用UITableViewCell對象

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{

    static NSString *identifier = @"UITableViewCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];

     }

    cell.textLabel.text = [NSString stringWithFormat:@"Text %i", indexPath.row];

    return cell;

}

UITableViewCell的常用屬性

設定背景

backgroundView

設定被選中時的背景視圖

selectedBackgroundView

selectionStyle屬性可設定UITableViewCell被選中時的背景顔色:

UITableViewCellSelectionStyleNone  沒有顔色

UITableViewCellSelectionStyleBlue  藍色(預設)

UITableViewCellSelectionStyleGray  灰色

自定義UITableViewCell

一般有兩種方式:

用一個xib檔案來描述UITableViewCell的内容

通過代碼往UITableViewCell的contentView中添加子視圖,在初始化方法(比如init、initWithStyle:reuseIdentifier:)中添加子控件,在layoutSubviews方法中配置設定子控件的位置和大小

UITableView的編輯模式

UITableView有個editing屬性,設定為YES時,可以進入編輯模式。在編輯模式下,可以管理表格中的行,比如改變行的排列順序、增加行、删除行,但不能修改行的内容

多種方式開啟編輯模式

@property(nonatomic,getter=isEditing) BOOL editing

- (void)setEditing:(BOOL)editing animated:(BOOL)animated

删除UITableView的行

首先要開啟編輯模式

實作UITableViewDataSource的如下方法:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

    // 如果UITableView送出的是删除指令

if (editingStyle == UITableViewCellEditingStyleDelete) {

        // 删除真實資料

         // [self.data removeObjectAtIndex:indexPath.row];

        // 删除UITableView中的某一行(帶動畫效果)

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];

        // 如果不考慮動畫效果,也可以直接[tableView reload];

    }

移動UITableView的行

實作UITableViewDataSource的如下方法(如果沒有實作此方法,将無法換行)

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath 

    int from = sourceIndexPath.row;

    int to = destinationIndexPath.row;

    if (from == to) return;

    // 交換資料

    // [self.data exchangeObjectAtIndex:from withObjectAtIndex:to];

選中UITableView的行

當某行被選中時會調用此方法(UITableViewDelegate的方法)

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

    //取消選中某一行,讓被選中行的高亮顔色消失(帶動畫效果)

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

UITableView常用方法

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style

初始化一個UITableView,并且設定表格樣式

- (void)reloadData   重新通路資料源,重新整理界面

- (NSInteger)numberOfSections  分區的個數

- (NSInteger)numberOfRowsInSection:(NSInteger)section

第section分區的行數

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

通過indexPath找到對應的UITableViewCell對象

是否要開啟編輯模式

- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated

取消選中某一行,讓被選中行的高亮顔色消失(帶動畫效果)

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier

通過identifier在(緩存)池中找到對應的UITableViewCell對象

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

移除indexPaths範圍内的所有行

@property(nonatomic,readonly) UITableViewStyle style 表格樣式

@property(nonatomic,assign) id  dataSource

資料源

@property(nonatomic,assign) id  delegate 代理

@property(nonatomic,getter=isEditing) BOOL editing 是否為編輯模式

@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle

設定分隔線的樣式

@property(nonatomic,retain) UIColor *separatorColor

設定分隔線的顔色

@property(nonatomic,retain) UIView *tableHeaderView

表頭顯示的視圖

@property(nonatomic,retain) UIView *tableFooterView

表尾顯示的視圖

@property(nonatomic) BOOL allowsSelection

是否允許選中行

@property(nonatomic) BOOL allowsSelectionDuringEditing

是否允許在編輯模式下選中行

@property(nonatomic) BOOL allowsMultipleSelection

是否允許選中多行

@property(nonatomic) BOOL allowsMultipleSelectionDuringEditing

是否允許在編輯模式下選中多行

UITableViewController

是UIViewController的子類,UITableViewController預設扮演了3種角色:視圖控制器、UITableView的資料源和代理

UITableViewController的view是個UITablView,由UITableViewController負責設定和顯示這個對象。UITableViewController對象被建立後,會将這個UITableView對象的dataSource和delegate指向UITableViewController自己

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html&gt;&lt;head&gt;&lt;meta http-equiv="Cont

一、UITableView

1.資料展示的條件

1&gt; UITableView的所有資料都是由資料源(dataSource)提供的,是以要想在UITableView展示資料,必須設定UITableView的dataSource資料源對象

2&gt; 要想當UITableView的dataSource對象,必須遵守UITableViewDataSource協定,實作相應的資料源方法

3&gt; 當UITableView想要展示資料的時候,就會給資料源發送消息(調用資料源方法),UITableView會根據方法傳回值決定展示怎樣的資料

2.資料展示的過程

1&gt; 先調用資料源的

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

得知一共有多少組

2&gt; 然後調用資料源的

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

得知第section組一共有多少行

3&gt; 然後調用資料源的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

得知第indexPath.section組 第indexPath.row 行顯示怎樣的cell(顯示什麼内容)

3.常見資料源方法

1&gt; 一共有多少組

2&gt; 第section組一共有多少行

3&gt; 第indexPath.section組 第indexPath.row行顯示怎樣的cell(顯示什麼内容)

4&gt; 第section組顯示怎樣的頭部标題

5&gt; 第section組顯示怎樣的尾部标題

4.tableView重新整理資料的方式

1&gt; 修改模型資料

2&gt; 重新整理表格

* reloadData 整體重新整理(每一行都會重新整理)

* - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

局部重新整理

5.性能優化

1&gt; 定義一個循環利用辨別

static NSString *ID = @"C1";

2&gt; 從緩存池中取出可循環利用的cell

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

3&gt; 如果緩存池中沒有可循環利用的cell

if (cell == nil) 

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];

4&gt; 覆寫cell上面的資料

cell.textLabel.text = [NSString stringWithFormat:@"第%d行資料", indexPath.row];

=====================================================

歡迎學習本文,未經部落客許可,禁止轉載!

繼續閱讀