- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
- (NSInteger)numberOfRowsInSection:(NSInteger)section
@interface xxxmodel : NSObject
...
@property (nonatomic, copy) str1;
@property (nonatomic, copy) str2;
@property (nonatomic, assign) num1;
@property (nonatomic, assign) num2;
...
@end
@implementation
@end
typedef NS_ENUM(NSUInteger, CompositeSubItemStatus) {
/**已结业*/
CompositeSubItemCompleted = 0,
/**未结业*/
CompositeSubItemNotSatisfied,
/**已完成*/
CompositeSubItemSubmit,
/**无状态*/
CompositeSubItemStatusNone
};
@interface CompositeSubItemModel : NSObject
...
/**是否结业、完成状态, default statusNone*/
@property (nonatomic, assign) CompositeSubItemStatus status;
...
@end
typedef NS_ENUM(NSUInteger, CompositeSubItemCellType) {
CompositeSubItemCellContainBody = 0,
CompositeSubItemCellNoBody = 0,
};
@property (nonatomic, copy) NSString *body;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
cell.separateLine.hidden = YES;
}
return cell;
}
//CompositeSubItemModel.h
@interface CompositeSubItemModel : NSObject
...
@property (nonatomic, weak) CompositeCourseStageModel *parentStage;
...
@end
//CompositeSubItemBaseCell.m
@implementation CompositeSubItemBaseCell
- (void)setItemModel:(JYVCompositeSubItemModel *)itemModel {
...
if (_itemModel == _itemModel.parentStage.items.lastObject) {
self.separateLine.hidden = YES;
} else {
self.separateLine.hidden = NO;
}
...
- }
@end
UIView *parent = [UIView new];
UIView *son = [UIView new];
[parent addSubview son];
@implementation CompositeCourseStageModel
- (void)setStageHeader:(CompositeStageHeaderModel *)stageHeader {
_stageHeader = stageHeader;
_stageHeader.parentStage = self;
}
- (void)setItems:(NSArray *)items {
_items = items.copy;
for (CompositeSubItemModel *item in items) {
item.parentStage = self;
}
}
@end
@interface CompositeCourseStageModel: NSObject
@property (nonatomic, assign) BOOL fold;
@end
@interface CompositeStageHeaderModel: NSObject
@property (nonatomic, assign) BOOL fold;
@end
header.parentStage.fold
@interface CompositeStageHeaderModel : NSObject
@property (nonatomic, assign) BOOL fold;
@end
@implementation CompositeStageHeaderModel
- (BOOL)fold {
return self.parentStage.fold;
}
- (void)setFold:(BOOL)fold {
self.parentStage.fold = fold;
}
@end
从设计一个可折叠tableview组件谈谈数据驱动UI (下篇)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//通过section计算出对应的segment和stage
...
if(isSegment) return 0;//恰好是segment最上层,只显示一个section header
//正常的stage
if (_dataSource[segment].fold || _dataSource[segment][stage].fold) { return 0; }
return _dataSource[segment][stage].items.count;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//通过section计算出对应的segment和stage
...
if(isSegment) return [SegmentHeaderView new];//segment必然需要展示header
if(!isSegment && !_datasource[segment].fold) return [Stageheader new];//stage的header在segment展开的状态才展示
return nil;
}
本文来自网易实践者社区,经作者朱建锋授权发布。