iOS--表視圖(UITableViewController)的基本概念和屬性,以及常用方法

一、表視圖的介紹

1、表視圖,是iOS中最重要的試圖,很多應用程序都會使用到,

2、表試圖里面可以放很多行信息

3、表視圖的兩種風格

1)普通風格

UITableViewStylePlain

2)分組風格

UITableViewStyleGrouped

3)UITableViewStylePlain和UITableViewStyleGrouped。這兩者操作起來其實并沒有本質(zhì)區(qū)別,只是后者按分組樣式顯示前者按照普通樣式顯示而已。

二、表視圖的基本結(jié)構(gòu)

1、表視圖有表頭、表尾、中間一連串單元格試圖組成

1)設置表頭

tableHeaderView

2)設置單元格試圖

UITableViewCell,單元格也可以分段顯示,每一段都可以通過代理設置段頭和段尾

2)設置表尾

tableFooterView

3) tableView的常用屬性和方法

設置表視圖分割線風格

@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle;

設置表視圖分割線顏色,默認標準灰色

@property(nonatomic,retain) UIColor *separatorColor;

設置表視圖的頭部視圖

@property(nonatomic,retain) UIView *tableHeaderView;

設置表視圖的尾部視圖

@property(nonatomic,retain) UIView *tableFooterView;

設置表視圖單元格的行高

@property(nonatomic) CGFloat rowHeight;

設置表視圖背景

@property(nonatomic, readwrite, retain) UIView *backgroundView

刷新表視圖單元格中數(shù)據(jù)

- (void)reloadData;

顯示指示條

showsVerticalScrollIndicator

設置表視圖section頭部行高

@property(nonatomic) CGFloat sectionHeaderHeight;

設置表視圖section尾部部行高

@property(nonatomic) CGFloat sectionFooterHeight

三、單元格的顯示

1、單元格的位置表示

NSIndexPath:能表示當前cell是tableView的第幾段第幾行

2、單元格的創(chuàng)建

UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

cell的樣式

1)UITableViewCellStyleDefault

左側(cè)顯示textLabel,imageView顯示在最左邊

2)UITableViewCellStyleValue1

左側(cè)顯示textLabel、右側(cè)顯示detailTextLabel,imageView顯示在最左邊

3)UITableViewCellStyleValue2

左側(cè)依次顯示textLabel(默認藍色)和detailTextLabel,imageView可選

4)UITableViewCellStyleSubtitle

左上方顯示textLabel,左下方顯示detailTextLabel,imageView顯示在最左邊

cell的輔助圖標 accessoryType

1) 不顯示任何圖標

UITableViewCellAccessoryNone,

2) 跳轉(zhuǎn)指示圖標

UITableViewCellAccessoryDisclosureIndicator

3) 內(nèi)容詳情圖標和跳轉(zhuǎn)指示圖標

UITableViewCellAccessoryDetailDisclosureButton

4) 勾選圖標

UITableViewCellAccessoryCheckmark

5) 內(nèi)容詳情圖標

UITableViewCellAccessoryDetailButton

5) 自定義輔助圖標

accessoryView屬性

3、cell的常用屬性

1)設置cell的背景試圖

backgroundView

2)設置選中的cellbei的背景圖片

selectedBackgroundView

3) 設置選中時的樣式

selectionStyle

練習:不分組的名人錄

四、數(shù)據(jù)源方法(UITableViewDatasource)

1、實例化表視圖時,必須要實現(xiàn)他的數(shù)據(jù)源方法,以此來完成表中數(shù)據(jù)的配置,一般來說數(shù)據(jù)源方法是用來配置表中的數(shù)據(jù)

2、常用數(shù)據(jù)源方法

1)配置section中含有行數(shù)

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

2)創(chuàng)建單元格實例

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

3) 配置表視圖section個數(shù),默認為1

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

4)section中的頭部視圖的標題

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

5)section中的尾部視圖的標題

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

/* 表視圖的編輯 移動、刪除等 */

6)指定單元格是否支持編輯

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

7)指定單元格是否支持移動

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

8)用戶編輯了哪一個單元格,在這里執(zhí)行刪除操作

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

9)實現(xiàn)此方法,移動單元格

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

五、代理方法(UITableViewDelegate)

1、一般是處理表視圖基本樣式(單元格高度)以及捕捉選中單元格事件

2、常用代理方法

1)配置行高

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

2)設置section 頭部、尾部視圖的高度

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

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

3)自定義section頭部、尾部視圖,注意:需要指定高度

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

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

4)用戶單擊單元格中輔助按鈕時,調(diào)用該方法

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

5)用戶單擊單元格,調(diào)用該方法

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

6)取消選中單元格時,調(diào)用該方法

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);

7)設置單元格編輯樣式

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

練習:分組的名字錄

1、設置段頭、段尾

2、自定義段頭段尾


表視圖常用屬性和方法

屬性

1、設置表視圖分割線風格

@property(nonatomic) UITableViewCellSeparatorStyle separatorStyle;

2、設置表視圖分割線顏色,默認標準灰色

@property(nonatomic,retain) UIColor *separatorColor;

3、設置表視圖的頭部視圖

@property(nonatomic,retain) UIView *tableHeaderView;

4、設置表視圖的尾部視圖

@property(nonatomic,retain) UIView *tableFooterView;

5、設置表視圖單元格的行高

@property(nonatomic) CGFloat rowHeight;

6、設置表視圖背景

@property(nonatomic, readwrite, retain) UIView *backgroundView

7、刷新表視圖單元格中數(shù)據(jù)

- (void)reloadData;

8、設置表視圖section頭部行高

@property(nonatomic) CGFloat sectionHeaderHeight;

9、設置表視圖section尾部部行高

@property(nonatomic) CGFloat sectionFooterHeight;

10、 刷新表視圖section中數(shù)據(jù)

- (void)reloadSectionIndexTitles

11、默認為NO,不可以編輯,設置時,不存在動畫效果

@property(nonatomic,getter=isEditing) BOOL editing;

12、覆蓋此方法,存在動畫效果

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

13、默認為YES,當表視圖不在編輯時,單元格是否可以選中

@property(nonatomic) BOOL allowsSelection NS_AVAILABLE_IOS(3_0);

14、默認為NO,當表視圖在編輯時,單元格是否可以選中

@property(nonatomic) BOOL allowsSelectionDuringEditing;

15、默認為NO,是否可以同時選中多個單元格,注意版本問題

@property(nonatomic) BOOL allowsMultipleSelection

17、 默認為NO,在編輯狀態(tài)下時,是否可以同時選中多個單元格,注意版本問題

@property(nonatomic) BOOL allowsMultipleSelectionDuringEditing

方法

1、指定一個cell,返回一個NSIndexPath實例,如果cell沒有顯示,返回nil

- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell;

2、指定一個范圍,返回一個數(shù)組,內(nèi)容是NSIndexPath實例,指定rect無效,返回nil

- (NSArray *)indexPathsForRowsInRect:(CGRect)rect;

3、指定一個NSIndexPath,返回一個cell實例,如果cell沒有顯示,返回為nil

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

4、根據(jù)顯示的cell,返回一組cell實例的數(shù)組,如果沒有顯示,返回nil

- (NSArray *)visibleCells;

5、根據(jù)顯示的cell,返回一組NSIndexPath實例的數(shù)組,如果沒有顯示,返回nil

- (NSArray *)indexPathsForVisibleRows;

6、滑動到指定的位置,可以配置動畫

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

7、插入一行cell,指定一個實現(xiàn)動畫效果

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

8、刪除一行cell, 指定一個實現(xiàn)動畫效果

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

9、刷新一個行cell,指定一個實現(xiàn)動畫效果

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);

10、移動cell的位置,指定一個實現(xiàn)動畫效果

- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath*)newIndexPath NS_AVAILABLE_IOS(5_0);


單元格的重用機制

試想如果這個表試圖有幾十個幾百行數(shù)據(jù)的話,如果我們每次都創(chuàng)建一個cell將會創(chuàng)建太多的對象,這樣會使你的軟件容易卡頓,如果這個cell上面在放有高清圖片的話更不可想象。

1)當表試圖顯示時,首先會創(chuàng)建幾個cell顯示在界面上

2)當一個cell被移出表視圖時,這個cell對象并不會被釋放,而是被放到一個緩存池中

3)當一個cell將要顯示時,我們先要在緩存池中查看緩存池中是否有cell,如果有的話,將這個cell對象取出來復用,如果沒有再重新創(chuàng)建

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容