- simpleCell:顯示簡(jiǎn)單的信息,點(diǎn)擊顯示/隱藏 detailCell
- detailCell:顯示詳細(xì)的信息,默認(rèn)不顯示
以前也做過(guò)這個(gè)功能,但我忘了以前是怎么做的。好像是借助了一個(gè)dictionary來(lái)記住那些Cell的detailCell已經(jīng)被展開(kāi)。懶得去查閱以前的代碼,重新開(kāi)始也許會(huì)更快一些。
simpleCell和detailCell之間的聯(lián)系是非常緊密,它們需要放置在一起。同時(shí),這兩者之間的變化不應(yīng)該影響到其他的cell,這兩個(gè)又需要獨(dú)立出來(lái)。對(duì)于tableView而言,這種關(guān)系,不就是section么。
每一組simpleCell和detailCell都?xì)w為一個(gè)section,需要展開(kāi)/收縮時(shí),更新相應(yīng)的section就可以,其他的不會(huì)影響到內(nèi)容。
這樣感覺(jué)好像比較簡(jiǎn)單。
//
// CTQProjectListViewController.m
// CTQProject
//
// Created by wangxuefeng on 16/6/15.
// Copyright ? 2016年 code. All rights reserved.
//
#import "CTQProjectListViewController.h"
#import "CTQProject.h"
#import "CTQProjectSimpleCell.h"
#import "CTQProjectDetailCell.h"
@interface CTQProjectListViewController ()
@property (strong, nonatomic) NSArray *dataSource;
@end
@implementation CTQProjectListViewController
- (void)viewDidLoad {
[super viewDidLoad];
CTQProject *p0 = [CTQProject new];
p0.open = YES;
CTQProject *p1 = [CTQProject new];
p1.open = NO;
CTQProject *p2 = [CTQProject new];
p2.open = NO;
self.dataSource = @[p0, p1, p2];
[self.tableView reloadData];
}
#pragma mark - UITableViewDelegate & UITableViewSource
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
CTQProject *project = self.dataSource[indexPath.section];
if (indexPath.row == 0) {
project.open = !project.open;
[self.tableView beginUpdates];
[self.tableView reloadSection:indexPath.section withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
CTQProject *project = self.dataSource[section];
return project.isOpen ? 2 : 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CTQProject *project = self.dataSource[indexPath.section];
if (indexPath.row == 0) {
CTQProjectSimpleCell *cell = [CTQProjectSimpleCell cellForTableView:tableView];
[XFLineHelper addBottomLineToView:cell];
return cell;
} else {
CTQProjectDetailCell *cell = [CTQProjectDetailCell cellForTableView:tableView];
[XFLineHelper addBottomLineToView:cell];
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
return [CTQProjectSimpleCell cellHeight];
} else {
return [CTQProjectDetailCell cellHeight];
}
}
@end