先說下實現(xiàn)的效果,用戶點擊某個分組,展示該分組的列表钟沛,在展示的同時奴艾,用戶不能通過上下滑動來滑動TableView看杭,在用戶點擊分組的時候,該分組會移動貼近導(dǎo)航欄。類似這樣的效果。思路和上一篇的UICollectionView分組類似涎跨,下面是效果圖:
qh_2.gif
實現(xiàn)的代碼全貼上去,有什么疑問可以在評論里說
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
{
NSInteger openSection;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *aArr;
@property (nonatomic, strong) NSMutableArray *bArr;
@property (nonatomic, strong) NSMutableArray *cArr;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) BOOL isDo;
@end
@implementation ViewController
static NSString *const cellID = @"cell";
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
}
return _tableView;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"分組";
_isDo = NO;
openSection = -1;
[self.view addSubview:self.tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.dataArr.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
button.backgroundColor = [UIColor cyanColor];
[button setTitle:[NSString stringWithFormat:@"%ld", section] forState:UIControlStateNormal];
[button addTarget:self action:sel_registerName("doOpen:") forControlEvents:UIControlEventTouchUpInside];
button.tag = section + 1000;
return button;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 44;
}
- (void)doOpen:(UIButton *)sender {
if (openSection == sender.tag - 1000) {
_isDo = !_isDo;
[self.tableView reloadData];
} else {
_isDo = NO;
openSection = sender.tag - 1000;
[self.tableView reloadData];
}
if (!_isDo) {
self.tableView.scrollEnabled = NO; // 打開狀態(tài) 不能滑動
[self.tableView setContentOffset:CGPointMake(0, openSection * 44 - 64) animated:YES];
} else {
self.tableView.scrollEnabled = YES; // 關(guān)閉狀態(tài) 能夠滑動
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == openSection) {
if (_isDo) { // YES 為打開過
return 0;
} // 沒打開過
return [self.dataArr[openSection] count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
cell.textLabel.text = [_dataArr[indexPath.section][indexPath.row] stringValue];
return cell;
}
- (NSMutableArray *)aArr {
if (!_aArr) {
_aArr = [NSMutableArray arrayWithObjects:@111,@222,@333,@444,@555,@666,@777, nil];
}
return _aArr;
}
- (NSMutableArray *)bArr {
if (!_bArr) {
_bArr = [NSMutableArray arrayWithObjects:@2222,@4444,@6666,@8888,@1111, nil];
}
return _bArr;
}
- (NSMutableArray *)cArr {
if (!_cArr) {
_cArr = [NSMutableArray arrayWithObjects:@33333,@55555,@77777,@99999,@11111,@22222,@33333, nil];
}
return _cArr;
}
- (NSMutableArray *)dataArr {
if (!_dataArr) {
_dataArr = [NSMutableArray arrayWithObjects:self.aArr,self.bArr,self.cArr, nil];
}
return _dataArr;
}
該分組效果是在某個App下看到的崭歧,于是就根據(jù)那個效果敲了這個分組給大家分享下隅很。
qh_1.png