先看效果圖

用法(與 UITableView 幾乎一致)
初始化 XDMultTableView
#import "XDMultTableView.h"
@property(nonatomic, readwrite, strong) XDMultTableView *tableView;
_tableView = [[XDMultTableView alloc] initWithFrame:CGRectMake(0, 64,
self.view.frame.size.width, self.view.frame.size.height - 64)];
_tableView.openSectionArray = [NSArray arrayWithObjects:@1, @2, nil];
_tableView.delegate = self;
_tableView.datasource = self;
_tableView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_tableView];
實作資料來源
- (NSInteger)mTableView:(XDMultTableView *)mTableView
numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (XDMultTableViewCell *)mTableView:(XDMultTableView *)mTableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [mTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
UIView *view = [[UIView alloc] initWithFrame:cell.bounds];
view.layer.backgroundColor = [UIColor whiteColor].CGColor;
view.layer.masksToBounds = YES;
view.layer.borderWidth = 0.3;
view.layer.borderColor = [UIColor lightGrayColor].CGColor;
cell.backgroundView = view;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(XDMultTableView *)mTableView {
return 6;
}
- (NSString *)mTableView:(XDMultTableView *)mTableView
titleForHeaderInSection:(NSInteger)section {
return @"我是表頭";
}
實作 delegate
- (CGFloat)mTableView:(XDMultTableView *)mTableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50;
}
- (CGFloat)mTableView:(XDMultTableView *)mTableView
heightForHeaderInSection:(NSInteger)section {
return 40;
}
- (void)mTableView:(XDMultTableView *)mTableView willOpenHeaderAtSection:(NSInteger)section {
NSLog(@"即將展開");
}
- (void)mTableView:(XDMultTableView *)mTableView willCloseHeaderAtSection:(NSInteger)section {
NSLog(@"即將關閉");
}
- (void)mTableView:(XDMultTableView *)mTableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"點擊 cell");
}
展開/收合的原理
核心只有一句話:在 section header 上掛一個點擊手勢,切換該 section 的展開狀態,再對 tableView 執行 insertRows / deleteRows。
先註冊手勢:
// section header view,把 section 存進 tag
view.tag = section;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(tapHeader:)];
[view addGestureRecognizer:tap];
再處理手勢回呼:
- (void)tapHeader:(UITapGestureRecognizer *)tap {
NSInteger section = tap.view.tag;
NSNumber *sectionObj = [NSNumber numberWithInteger:section];
UIImageView *imageView = (UIImageView *)[tap.view viewWithTag:100];
// _multopenSectionArray 記錄每個 section 的展開/收合狀態
if ([_multopenSectionArray containsObject:sectionObj]) {
NSArray *deleteArray = [self buildDeleteRowWithSection:section];
[_multopenSectionArray removeObject:sectionObj];
// 要收合的 section 對應的所有 indexPath
[_tableView deleteRowsAtIndexPaths:deleteArray
withRowAnimation:UITableViewRowAnimationFade];
[UIView animateWithDuration:0.3 animations:^{
imageView.transform = CGAffineTransformMakeRotation(-M_PI / 2);
}];
} else {
[_multopenSectionArray addObject:sectionObj];
// 要展開的 section 對應的所有 indexPath
NSArray *insertArray = [self buildInsertRowWithSection:section];
[_tableView insertRowsAtIndexPaths:insertArray
withRowAnimation:UITableViewRowAnimationFade];
[UIView animateWithDuration:0.3 animations:^{
imageView.transform = CGAffineTransformMakeRotation(0);
}];
}
}
實作上的幾個關鍵點
1. buildDeleteRowWithSection: 與 buildInsertRowWithSection: 要依「該 section 前面已經展開了幾組列」來累加 offset 產生正確的 indexPath,這是最容易出錯的地方,建議畫圖推一遍。
2. 用一個集合(這裡是 _multopenSectionArray)記錄展開狀態,比在 cell 裡存狀態可靠,也比較好除錯。
3. insert / delete 必須包在 beginUpdates 與 endUpdates 之間(iOS 11 之後可以省略,但同時增刪多組時仍建議保留),否則容易出現內部資料與畫面不一致的例外。
4. 箭頭旋轉動畫只是加分項,別忘了在重複使用 header 時把 transform 重設,避免狀態殘留。
以上就是 iOS 展開二級列表效果的完整實作,希望對大家開發有幫助。