編輯:關於 Android 程式設計
QQ 的聯絡人清單點一下分組標題就能收合或展開,這個效果在 UITableView 上實作起來比想像中簡單。一開始容易想到的路子是「在 cell 裡再嵌一個 UITableView」,但巢狀 TableView 的滾動與重用問題會讓你頭痛很久。
換個角度想就通了:折疊與展開本質上只是「改變某個 section 的 row 數量」。而點擊的入口,用 section 的 HeaderView 加上手勢判斷剛剛好。
需要三個屬性:資料源、各組標題,以及一個記錄「哪一組目前是展開狀態」的字典。
@property (nonatomic, strong) UITableView *myTableView; @property (nonatomic, strong) NSMutableArray *listArray; // 資料源 @property (nonatomic, strong) NSMutableArray *titlesArray; // 分組名稱 @property (nonatomic, strong) NSMutableDictionary *openSectionDict; // 記錄展開狀態
- (void)viewDidLoad {
[super viewDidLoad];
_myTableView = [[UITableView alloc] initWithFrame:self.view.frame
style:UITableViewStyleGrouped];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
[self.view addSubview:_myTableView];
self.openSectionDict = [[NSMutableDictionary alloc] init];
[self setUpData];
}
- (void)setUpData {
self.listArray = [NSMutableArray new];
self.titlesArray = [NSMutableArray new];
for (int i = 0; i < 5; i++) {
[self.titlesArray addObject:[NSString stringWithFormat:@"section %d", i]];
NSMutableArray *array = [NSMutableArray new];
for (int j = 0; j < 4; j++) {
[array addObject:[NSString stringWithFormat:@"row %d", j]];
}
[self.listArray addObject:array];
}
}
整個效果的樞紐就在這個方法——如果該組在字典中記為未展開,就回傳 0 個 row:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger opened = [[self.openSectionDict valueForKey:[NSString stringWithFormat:@"%ld", (long)section]] integerValue];
return opened == 0 ? 0 : 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL_ID"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:@"CELL_ID"];
}
cell.textLabel.text = [NSString stringWithFormat:@"row %ld", (long)indexPath.row];
return cell;
}
用 tag 把 section 編號藏在 HeaderView 上,點擊時就能反推是哪一組被按了:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 45;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.1; // 避免 Grouped 樣式出現預設間距
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(
0, 0, tableView.bounds.size.width, 40)];
view.backgroundColor = [UIColor whiteColor];
view.tag = KTAG + section; // 用 tag 記住 section 編號
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(
20, 0, view.bounds.size.width, view.bounds.size.height)];
label.text = self.titlesArray[section];
[view addSubview:label];
BOOL opened = [[self.openSectionDict valueForKey:
[NSString stringWithFormat:@"%ld", (long)section]] integerValue] != 0;
UIImageView *arrow = [[UIImageView alloc] initWithFrame:
opened ? CGRectMake(10, (view.bounds.size.height - 7) / 2, 10, 7)
: CGRectMake(10, (view.bounds.size.height - 10) / 2, 7, 10)];
arrow.image = opened ? [UIImage imageNamed:@"Triangle_down_gray"]
: [UIImage imageNamed:@"Triangle_right_gray"];
[view addSubview:arrow];
UITapGestureRecognizer *tap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(collegeTaped:)];
[view addGestureRecognizer:tap];
return view;
}
- (void)collegeTaped:(UITapGestureRecognizer *)sender {
NSString *key = [NSString stringWithFormat:@"%ld", (long)(sender.view.tag - KTAG)];
// 狀態取反
if ([[self.openSectionDict objectForKey:key] integerValue] == 0) {
[self.openSectionDict setObject:@"1" forKey:key];
} else {
[self.openSectionDict setObject:@"0" forKey:key];
}
NSIndexSet *set = [NSIndexSet indexSetWithIndex:sender.view.tag - KTAG];
[self.myTableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
}
這裡用 reloadSections:withRowAnimation: 而不是 reloadData,有兩個好處:只重繪受影響的那一組,效能更好;而且能帶入 Fade 動畫,折疊的過渡不會硬邦邦。

目前 openSectionDict 用 0/1 表示收合狀態,若日後要做「同一時間只能展開一組」的手風琴效果,只要在切換時先把其他 key 全部設為 0 即可。另外若資料是異步載入,記得在回來後呼叫 reloadData,並把 openSectionDict 的內容保留下來,使用者捲動時才不會跳回全收合的狀態。
突破Android 64K方法限制:MultiDex配置與構建優化指南
什麼是 64K 方法限制 在 Android 應用構建過程中,如果代碼與依賴庫過多,可能會遇到方法數超限的錯誤。這個限制來自單個 dex 文件可引用的方法總數上限,通常被
Android 實戰:仿超級課程表的一鍵匯入課表功能
課程表 App 的一鍵匯入功能,核心在於模擬登入教務系統、抓取課程資料並解析顯示。本文以仿作方式說明流程。 準備工具 可使用 HttpWatch 分析登入與查詢請求,使
IntentService 源碼解析:從使用到原理
IntentService(Service)的使用場景 IntentService 與 Service 的區別 IntentService 的使用 IntentServi
移動端 HTML5:去掉 input 日期輸入框的默認白底與邊框(兼容 Android 與 iOS)
前兩天做一個在微信內打開的 HTML5 頁面,其中有個訂單查詢需要選擇日期。一開始直接用了 <input type=date>,沒加任何樣式,結果渲染出來是白