一、UITableView的代理方法

#pragma mark 每一行的高度

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

#pragma mark 選中了某一行就會呼叫

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

#pragma mark 取消選中了某一行就會呼叫

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

#pragma mark 當用戶提交了一個編輯操作就會呼叫(比如點選了“刪除”按鈕)

// 只要實現了這個方法,就會預設新增滑動刪除功能

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

#pragma mark 當移動了某一行cell就會呼叫

// 只要實現了這個方法,就會預設新增排序功能

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

二、修改Cell的狀態

1.最好通過“修改模型資料”來修改Cell的狀態

2.修改步驟

1> 修改模型資料

2> 重新整理表格

* 整體重新整理:reloadData(最重要)

* 區域性重新整理:reloadRowsAtIndexPaths:withRowAnimation:

三、UITableView常見方法

1.取消選中某一行(去掉cell選中時預設的藍色背景)

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

2.區域性重新整理(僅僅重新整理indexPaths陣列中裝著的行)

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

3.整體重新整理(螢幕中的每一行都重新整理)

- (void)reloadData;

4.直接刪除介面上的行數(要求模型資料也要刪掉對應的數量)

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

5.設定編輯模式

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

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

// 注意:

不管是區域性重新整理,還是整體重新整理,原理都是:

UITableView重新向資料來源(dataSource)和代理(delegate)傳送相應的訊息,最終將得到的資料展示出來

1.常見錯誤解析

1.1

錯誤原因:說ViewController 沒有實現 tableView:numberOfRowsInSection:

解決方式:實現tableView:numberOfRowsInSection:

1.2

UITableView內部實現原理:

資料來源實現了這個方法

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

tableView內部自動會呼叫以下方法新增cell。

[tableView addSubview:cell];

如果返回的cell為空,也就意味著生成下面一行程式碼。

[tableView addSubview:nil];

而addSubview是將右邊引數新增到陣列中儲存起來,而陣列是不能新增空值的。所有集合物件都不能出傳空。例如陣列,字典,NSSet。

以上錯誤總結:作為tableView的資料來源必須實現兩個方法。

返回行數

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

返回每一行顯示的內容

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

另外返回每一行顯示的內容不能返回nil。

2.淘寶介面例子思路

UITableView開發模式都是先將資料轉換為模型。

步驟一:根據plist檔案建立模型物件

步驟二,解析plist檔案,將檔案中的資料轉換為模型物件。

步驟三:將模型物件用一個數組儲存起來

步驟四:實現tableView的資料來源方法。(設定資料來源,遵守資料來源協議)

步驟五:將字典轉化為模型的操作封裝到模型裡面去。

步驟六:根據設計角度的上面思考,需要實現兩個方法。

步驟七:實現tableView的代理方法。(設定代理,遵守代理協議)

步驟八:點選某一行打鉤實現步驟:

  1. 用一個數組記錄住選中的模型(如果模型在陣列中了就刪除,沒有就新增)
  2. 重新整理表格

步驟九:在返回每一行的方法中,新增一個判斷來決定是否cell需要打鉤

步驟十:更改UILabel的顯示

步驟十一:監聽刪除按鈕的點選

  1. 刪除模型
  2. 重新整理表格
  3. 更改標題
  4. 清空被選中模型的陣列
  5. 讓刪除按鈕不能點選

3.事件處理補充:UILable不能處理事件。因此把UILabel放在按鈕上面也能點選,他會把事件傳遞給下一個控制元件處理。

4.重新整理表格行的方法.

使用注意:必須前後資料保持一致才能呼叫這些方法,否則會報錯。

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

-(void)reloadSections:(NSIndexSet*)sections withRowAnimation:(UITableViewRowAnimation)animation;

使用注意:必須資料刪除多少,表格刪除多少,前後資料不一致才能呼叫這些方法,否則會報錯。

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

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

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

這個重新整理方法,沒有限制

- (void)reloadData;

5.UITableView編輯模式刪除功能小例子思路

步驟一:搭建頁面

步驟二:實現資料來源方法。(設定資料來源,遵守資料來源協議)

步驟三:建立資料模型

步驟四:建立陣列儲存資料模型

步驟五:監聽刪除按鈕,點選刪除按鈕進入編輯模式

步驟六:改本地化

步驟七:實現代理方法。

當用戶提交一個編輯操作就會呼叫。實現這個方法還會預設支援滑動刪除

  1. 刪除模型資料
  2. 重新整理表格

步驟八:實現排序功能

  1. 調整模型資料裡的順序
  2. 取出要拖動的模型資料
  3. 先刪除要拖動的模型資料
  4. 然後將拖動的模型資料插入新的位置

6.UITableViewCell的結構:UITableViewCell外面有一層ContentView,以後要往UITableViewCell裡新增子控制元件,新增到ContentView上面。

7.MVC

模型中不能擁有控制器和檢視,模型有可能用來重用的,包含控制器就不能重用了,因為模型脫離控制器就不能使用了。

檢視不能直接訪問模型。

檢視和模型的聯絡都是通過控制器。

控制器向模型拿到資料展示到檢視上。