您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 開發入門:打造你的第一個簡單表視圖

編輯:iOS 開發綜合

表視圖(UITableView)是 iOS 開發中使用頻率最高的視圖之一。無論是通訊錄還是頻道列表,我們通常都會選擇以列表形式來呈現資料。表視圖的分段、分組、索引等功能,讓資料看起來更規整、更有條理;更棒的是,它還能透過細節展示等多層次方式來組織資料,可謂「一表勝千言」。

相較於其他控制項,表視圖的使用略顯複雜,但對照它靈活多變的能力,這份投入絕對值得。

什麼是簡單表視圖

本節從最基礎的動態表說起(在 iOS 5 之前全部都是動態表,尚無動態表與靜態表之分)。

建立簡單表視圖

iOS 5 之後可以使用 xib 或 Storyboard 來建立表視圖。若要顯示一個最基本的表,只需實作 UITableViewDataSource 協議中兩個必須實作的方法:tableView:numberOfRowsInSection: 與 tableView:cellForRowAtIndexPath:。

其中,initWithFrame:style: 是在實例化表視圖時呼叫;若採用 xib 或 Storyboard 設計,則表視圖會在實例化視圖控制器時一併建立。表視圖顯示時會發送 tableView:numberOfRowsInSection: 訊息來詢問每個節的行數;單元格顯示時則會發送 tableView:cellForRowAtIndexPath: 訊息來取得顯示資料。

以下建立一個顯示世界盃球隊資訊的簡單表視圖,使用預設樣式,包含圖標與主標題:

1. 用「Single View Application」模板建立名為「SimpleTable」的工程。
2. 在 Interface Builder 中刪除預設的 View Controller,從物件庫拖入一個「Table View Controller」。
3. 將 ViewController.h 中的父類從 UIViewController 改為 UITableViewController。
4. 在 Table View Controller 的 Identity Inspector 中,把 Class 設為我們自己撰寫的「ViewController」。
5. 在 Table View 的屬性檢查器中,Content 有「Dynamic Prototypes」與「Static Cells」兩個選項,這裡選擇「Dynamic Prototypes」來建構動態表。

單元格的建立與重用

若改用程式碼建立單元格,需把 Prototype Cells 設為 0,並採用可重用的寫法:

static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

這裡的 Identifier 是可重用單元格標識符,概念與 Collection View 中的可重用單元格相同:先嘗試從表視圖中找出可重用的單元格,若沒有才透過 initWithStyle:reuseIdentifier: 建立新的單元格。若改以 Storyboard 設計單元格,則程式碼中不必再自行實例化,直接依設定的 Identifier 取得實例即可達到重用目的。

載入資料

先把 team.plist 與球隊圖片加入工程。在 ViewController.h 中定義一個用來裝載資料的陣列屬性:

#import <UIKit/UIKit.h>

@interface ViewController : UITableViewController
@property (nonatomic, strong) NSArray *listTeams;
@end

在 viewDidLoad 中讀取屬性列表檔:

- (void)viewDidLoad {
  [super viewDidLoad];
  NSBundle *bundle = [NSBundle mainBundle];
  NSString *plistPath = [bundle pathForResource:@"team" ofType:@"plist"];
  self.listTeams = [[NSArray alloc] initWithContentsOfFile:plistPath];
}

實作資料來源協議

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return [self.listTeams count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"CellIdentifier";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  }
  NSUInteger row = [indexPath row];
  NSDictionary *rowDict = [self.listTeams objectAtIndex:row];
  cell.textLabel.text = [rowDict objectForKey:@"name"];
  NSString *imagePath = [rowDict objectForKey:@"image"];
  imagePath = [imagePath stringByAppendingString:@".png"];
  cell.imageView.image = [UIImage imageNamed:imagePath];
  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  return cell;
}

由於此表實際上只有一個節,因此 numberOfRowsInSection: 直接回傳 listTeams 的長度即可。在 cellForRowAtIndexPath: 中,indexPath 的 row 方法可取得目前的行索引,而 cell.accessoryType 則用於設定擴展視圖的類型。

最後,你可以把單元格樣式 UITableViewCellStyleDefault 替換成其他三種樣式,親自體驗它們各自的顯示效果,加深對表視圖的認識。

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