你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS 表格 UITableView 學習筆記

IOS 表格 UITableView 學習筆記

編輯:IOS開發綜合

UITableView是UIScrollView的子類,因此UITableView可以響應滾動事件


要使用UITableView需要讓控制器實現UITableViewDataSource 協議 或者讓控制器 繼承 UITableViewController 它已經實現UITableViewDataSource 和 UITableViewDelegate 代理協議

使用UITableViewDataSource “必須實現的方法” 俗稱配置數據源

// 每一組顯示多少行

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

// 每一行顯示什麼內容 默認當模擬器啟動的時候只顯示可視范圍的單元格(cell)內容數據 當有一個新的cell進入視野范圍,就會調用它 創建一個新的單元格(Cell), 當不可視的時候會進行自動釋放。

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

1.首先在加載的時候要讓tableView的數據源與控制器關聯起來

- (void)viewDidLoad

{

[superviewDidLoad];

self.tableView.dataSource=self;

}

UITableViewDataSource的可選方法

// 如果想改變tableView有幾組要實現下面這個方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // 默認只有1組

// 第section組顯示什麼標題

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

// 第section組顯示什麼尾部標題

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

// 局部刷新表格 reloadRowsAtIndexPaths

NSIndexPath *path = [NSIndexPathindexPathForRow:row inSection:0];

[self.tableViewreloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationBottom];

// 重新加載數據 用這個方法可實現加載更多數據

[self.tableViewreloadData];

// 調用上面這個方法的時候 會調用

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

方法進行重新賦值


// 返回右邊索引條顯示的字符串數據

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

UITableView的代理協議 UITableViewDelegate

通常處理tableView的事件都在代理協議中實現要使用UITableViewDelegate

需要在加載的時候設置

- (void)viewDidLoad

self.tableView.delegate =self;

// 設置每一行的高度不同

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

// 當某一行被選中的時候

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

{

// 獲取第幾行被選中

NSInteger *rowx = indexPath.row;

}

// 當表格開始拖拽的時候觸發

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;

// 每一組頭部顯示的視圖

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

UITableView常用屬性

// 在表格頭部增加一個控件

self.tableView.tableHeaderView = [UIButtonbuttonWithType:UIButtonTypeContactAdd];

// 在表格尾部增加一個控件

self.tableView.tableFooterView = [[UISwitchalloc] init];

// 設置表格統一行高

self.tableView.rowHeight = 60;

// 設置每組頭的行高

self.tableView.sectionHeaderHeight = 44;

// 設置表格背景視圖

self.tableView.backgroundView =nil;

// 設置表格背景顏色

self.tableView.backgroundColor = [UIColorcolorWithRed:0.9 green:0.9blue:0.9 alpha:1];

// 設置表格不允許被選中

self.tableView.allowsSelection =NO;


UITableView優化

需要優化的方法

// 每當有一個cell進入視野范圍內,就會調用此方法, 當cell不可見的時候會進行自動釋放 因此在屏幕中始終只有一個cell 在變化 所以我們要對這個cell的內存進行重用 實現如下

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

{

staticNSString *ID = @"hero";

// 1.通過一個標識去緩存池中尋找可循環利用的cell

// dequeue :出列 (查找)

UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID];

// 2.如果沒有可循環利用的cell

if (cell ==nil){

cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:ID];

// NSLog(@"------緩存池找不到cell--%d",indexPath.row);

}

// 3.給cell設置新的數據

// 取出模型

Hero *hero =self.heros[indexPath.row];

// 設置cell的數據

cell.textLabel.text = hero.name;

cell.detailTextLabel.text = hero.intro;

cell.imageView.image = [UIImageimageNamed:hero.icon];

return cell;

}

2.使用默認的UItableViewCell會非常影響性能,奇怪的是使用自定義的View,而非預定義的UItableViewCell,會快一些

UITableView靜態的Cell

默認我們從 設計界面 拖進來的是動態的Cell

靜態的Cell 可以直接在cell上面進行拖動控件進行自定義cell

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved