- simpleCell:顯示簡單的信息,點擊顯示/隱藏 detailCell
- detailCell:顯示詳細的信息,默認不顯示
以前也做過這個功能,但我忘了以前是怎么做的。好像是借助了一個dictionary來記住那些Cell的detailCell已經被展開。懶得去查閱以前的代碼,重新開始也許會更快一些。
simpleCell和detailCell之間的聯系是非常緊密,它們需要放置在一起。同時,這兩者之間的變化不應該影響到其他的cell,這兩個又需要獨立出來。對于tableView而言,這種關系,不就是section么。
每一組simpleCell和detailCell都歸為一個section,需要展開/收縮時,更新相應的section就可以,其他的不會影響到內容。
這樣感覺好像比較簡單。
//
// 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