本文整理 UITableView 的核心知識點,適合剛接觸 iOS 列表開發的朋友參考。UITableView 繼承自 UIScrollView,因此天然支持滾動,只是方向被限制為縱向。
一個 UITableView 由若干 section 與 cell 組成,顯示內容來自數據源,通常由 ViewController 擔任代理,因此需要遵循 UITableViewDataSource 與 UITableViewDelegate 兩個協議。
簡單的實現步驟如下:
1. 將 UITableView 加入視圖,可選擇 Plain 或 Grouped 兩種風格;
2. 把 dataSource 設為 ViewController,並讓其遵循 UITableViewDataSource 協議;
3. 通過 numberOfSectionsInTableView 返回 section 的數量;
4. 通過 numberOfRowsInSection 返回每個 section 的行數;
5. 用 titleForHeaderInSection 與 titleForFooterInSection 設置首尾部標題;
6.1 在 cellForRowAtIndexPath 中配置 cell 內容,透過傳入的 NSIndexPath 定位具體行,最終返回一個 UITableViewCell 對象。
6.2 NSIndexPath 同時包含 section 與 row,能精準定位到哪個 section 的哪一行。
6.3 取到的數據最終賦值給 UITableViewCell 中 textLabel 的 text 屬性。
UITableViewCellStyle 共有四種樣式,設為 UITableViewCellStyleSubtitle 時可以顯示副標題說明。
同樣在 cellForRowAtIndexPath 中,還可以進一步配置:
6.4 透過 imageView 屬性為每個 cell 設置圖標;
6.5 副標題對應 detailTextLabel,是個 UILabel,可設置內容與顏色;
6.6 accessoryType 可設置右側的選項或箭頭指示樣式。
當數據模型發生變化時,需要通知視圖刷新,分為全部刷新與局部刷新兩種:
1. reloadData 刷新全部數據;
2. reloadRowsAtIndexPaths 刷新局部數據。
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
其中 indexPaths 是 NSIndexPath 數組,animation 為 UITableViewRowAnimation 枚舉,用來指定刷新時的動畫效果。
前面提到 cellForRowAtIndexPath 中會實例化 UITableViewCell,構造方法如下:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
reuseIdentifier 就是複用標識符。滑出屏幕的 cell 會被丟入緩存池,即將顯示的 cell 會先按標識符到緩存池中查找,命中就直接複用,避免反覆實例化帶來的內存與性能開銷;若未找到才新建一個。具體代碼如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *reuseIdentifierStr = @"myCell"; //在緩沖池中尋找 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifierStr]; //如果沒找到,就實例化一個新的cell if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifierStr]; } Product *p = data[indexPath.row]; cell.textLabel.text = p.name; cell.imageView.image = [UIImage imageNamed:p.icon]; cell.detailTextLabel.text = p.detail; cell.detailTextLabel.textColor = [UIColor grayColor]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; }7. 借助 Toolbar 的添加、刪除按鈕可以實現列表項的增刪。
7.1 在按鈕回調中設置 UITableViewCellEditingStyle 標識,1 為刪除、2 為添加。由於該枚舉只讀,只能透過給 tableview 的 tag 賦值來區分操作類型。
- (IBAction)action:(UIBarButtonItem *)sender { if (sender.tag == 1) { _tableView.tag = UITableViewCellEditingStyleDelete; } else{ _tableView.tag = UITableViewCellEditingStyleInsert; } BOOL isEdit = _tableView.isEditing; [_tableView setEditing:!isEdit]; }7.2 標識需在 editingStyleForRowAtIndexPath 的返回值中給出,系統才會知道是刪除還是添加。
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ return _tableView.tag; }7.3 具體的增刪邏輯則在 commitEditingStyle 中實現。
- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ //刪除操作 if (editingStyle == 1) { //刪除數據 [_dataList removeObjectAtIndex:indexPath.row]; //更新cell [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight]; }else{ //添加數據 [_dataList insertObject:@"新建數據" atIndex:indexPath.row + 1]; //更新cell NSIndexPath *insertIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section]; [tableView insertRowsAtIndexPaths:@[insertIndexPath] withRowAnimation:UITableViewRowAnimationTop]; } }