UITableView 是 iOS 最常被使用的列表元件。要讓它動起來,只需要三件事:提供資料來源、告訴它每一組有幾列、再回傳每一列的樣子。
#import <Foundation/Foundation.h> @interface NJHero : NSObject /** 頭像 */ @property (nonatomic, copy) NSString *icon; /** 名稱 */ @property (nonatomic, copy) NSString *name; /** 描述 */ @property (nonatomic, copy) NSString *intro; - (instancetype)initWithDict:(NSDictionary *)dict; + (instancetype)heroWithDict:(NSDictionary *)dict; @end
#import "NJViewController.h"
#import "NJHero.h"
@interface NJViewController () <UITableViewDataSource, UITableViewDelegate>
/** 保存所有的英雄數據 */
@property (nonatomic, strong) NSArray *heros;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation NJViewController
#pragma mark - 懶加載
- (NSArray *)heros
{
if (_heros == nil) {
// 1. 取得完整路徑
NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"heros" ofType:@"plist"];
// 2. 依路徑載入數據
NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath];
// 3. 字典轉模型
NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count];
for (NSDictionary *dict in dictArray) {
NJHero *hero = [NJHero heroWithDict:dict];
[models addObject:hero];
}
// 4. 賦值
_heros = [models copy];
}
return _heros;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.rowHeight = 60; // 高度一致時,直接用屬性設定
self.tableView.delegate = self;
}
#pragma mark - UITableViewDataSource
// 有幾組
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// 每一組有幾列
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return self.heros.count;
}
// 每一列顯示什麼
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:nil];
NJHero *hero = self.heros[indexPath.row];
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.intro;
cell.imageView.image = [UIImage imageNamed:hero.icon];
return cell;
}
#pragma mark - 控制狀態列
/** 回傳 YES 代表隱藏狀態列,NO 相反 */
- (BOOL)prefersStatusBarHidden
{
return YES;
}
@end
預先配置容量:[NSMutableArray arrayWithCapacity:dictArray.count] 事先配置好空間,能避免動態擴容帶來的效能損耗。
設定 Cell 高度的三種方式:
self.tableView.rowHeight = 60;——適用於高度一致。heightForRowAtIndexPath:——高度不一致時使用。- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (1 == indexPath.row) {
return 180;
}
return 44;
}
reuseIdentifier 傳 nil 的代價:教學程式碼為了簡潔常傳 nil,但正式專案一定要給 Cell 註冊識別碼並改用 dequeueReusableCellWithIdentifier:,否則長列表會因為不斷配置新 Cell 而嚴重掉幀。
在 cellForRowAtIndexPath: 中,除了文字與圖片,還有幾個實用屬性:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:nil];
NJHero *hero = self.heros[indexPath.row];
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.intro;
cell.imageView.image = [UIImage imageNamed:hero.icon];
// 2.3 輔助視圖:accessoryType 只能選系統樣式
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// accessoryView 可以放任何自訂 view
if (0 == indexPath.row) {
cell.accessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
} else {
cell.accessoryView = [[UISwitch alloc] init];
}
// 2.4 背景顏色
cell.backgroundColor = [UIColor blueColor];
// 常態背景:可放自訂 view
UIView *view2 = [[UIView alloc] init];
view2.backgroundColor = [UIColor purpleColor];
cell.backgroundView = view2;
// 或直接放一個 UIImageView
UIImageView *iv = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"buttondelete"]];
cell.backgroundView = iv;
// 選取狀態下的背景
UIView *selectedBg = [[UIView alloc] init];
selectedBg.backgroundColor = [UIColor purpleColor];
cell.selectedBackgroundView = selectedBg;
return cell;
}
accessoryType 只能挑系統內建的幾種樣式(箭頭、勾選、加號等),設定簡單但不客製;accessoryView 則接受任意 UIView,彈性最大。兩者不可同時使用,設了 accessoryView 就會覆蓋 accessoryType。
backgroundView 管的是常態下的背景,selectedBackgroundView 管的是被點下去那一瞬間的背景,兩者分工明確,不要搞混。