diff --git a/DEWTableDynamicSection.h b/DEWTableDynamicSection.h new file mode 100644 index 0000000..6234d52 --- /dev/null +++ b/DEWTableDynamicSection.h @@ -0,0 +1,52 @@ +// +// DEWTableDynamicSection.h +// DEWTableViewManager +// +// Created by Shaw on 2019/1/17. +// Copyright © 2019 Forcewith Co., Ltd. All rights reserved. +// + +#import +#import "DEWTableSectionProtocol.h" + +typedef void(^DEWTableConfigureBlock)(id cell, id data); +typedef void(^DEWTableSelectBlock)(NSIndexPath *indexPath); + +typedef BOOL(^DEWDynaicSectionCanEditBlock)(NSIndexPath *indexPath); +typedef UITableViewCellEditingStyle(^DEWDynaicSectionEditingStyleBlock)(NSIndexPath *indexPath); +typedef void(^DEWDynaicSectionCommitEditingBlock)(UITableViewCellEditingStyle editingStyle, NSIndexPath *indexPath); + +@interface DEWTableDynamicSection : NSObject + +//protocol +@property (nonatomic, strong) UIView *sectionHeader; +@property (nonatomic, strong) UIView *sectionFooter; + +@property (nonatomic, copy) DEWTableConfigureBlock cellConfigure; +@property (nonatomic, copy) DEWTableSelectBlock cellSelected; + +@property (nonatomic, assign) NSInteger numberOfRows; + +// + +@property (nonatomic, strong) NSArray *dataArray; + +@property (nonatomic, assign) CGFloat rowHeight; +@property (nonatomic, strong) NSString *idenfier; + +@property (nonatomic, copy) DEWDynaicSectionCanEditBlock canEdit; +@property (nonatomic, copy) DEWDynaicSectionEditingStyleBlock editingStyle; +@property (nonatomic, copy) DEWDynaicSectionCommitEditingBlock commitEditing; + + +- (instancetype)initWithCellIdenfier:(NSString *)idenfier + dataArray:(NSArray *)dataArray + configure:(DEWTableConfigureBlock)configureBlock + didSelected:(DEWTableSelectBlock)selectedCellBlock; + + +- (instancetype)init NS_UNAVAILABLE; + +@end + + diff --git a/DEWTableDynamicSection.m b/DEWTableDynamicSection.m new file mode 100644 index 0000000..7a8895b --- /dev/null +++ b/DEWTableDynamicSection.m @@ -0,0 +1,93 @@ +// +// DEWTableDynamicSection.m +// DEWTableViewManager +// +// Created by Shaw on 2019/1/17. +// Copyright © 2019 Forcewith Co., Ltd. All rights reserved. +// + +#import "DEWTableDynamicSection.h" + +@implementation DEWTableDynamicSection + +- (instancetype)initWithCellIdenfier:(NSString *)idenfier + dataArray:(NSArray *)dataArray + configure:(DEWTableConfigureBlock)configureBlock + didSelected:(DEWTableSelectBlock)selectedCellBlock; +{ + + self = [super init]; + if (!self) return nil; + + self.idenfier = idenfier; + self.cellConfigure = [configureBlock copy]; + self.cellSelected = [selectedCellBlock copy]; + + if (dataArray) { + self.dataArray = dataArray; + }else { + self.dataArray = [[NSArray alloc]init]; + } + + return self; +} + +- (NSInteger)numberOfRows +{ + return self.dataArray.count; +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath +{ + + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.idenfier forIndexPath:indexPath]; + if (self.cellConfigure) { + id data = [self.dataArray objectAtIndex:indexPath.row]; + self.cellConfigure(cell, data); + } + return cell; +} + +- (void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath +{ + if (self.cellSelected) { + self.cellSelected(indexPath); + } +} + +- (CGFloat)heightForRowAtIndexPath:(NSIndexPath *)indexPath +{ + + if (self.rowHeight) { + return self.rowHeight; + } + + return UITableViewAutomaticDimension; +} + +- (BOOL)canEditRowAtIndexPath:(NSIndexPath *)indexPath +{ + if (self.canEdit) { + return self.canEdit(indexPath); + }else { + return NO; + } +} + +- (UITableViewCellEditingStyle)editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath +{ + if (self.editingStyle) { + return self.editingStyle(indexPath); + }else { + return UITableViewCellEditingStyleNone; + } +} + +- (void)commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath +{ + if (self.commitEditing) { + self.commitEditing(editingStyle, indexPath); + } +} + +@end diff --git a/DEWTableManager.h b/DEWTableManager.h new file mode 100644 index 0000000..f927f40 --- /dev/null +++ b/DEWTableManager.h @@ -0,0 +1,28 @@ +// +// DEWTableManager.h +// DEWTableViewManager +// +// Created by Shaw on 2019/1/17. +// Copyright © 2019 Forcewith Co., Ltd. All rights reserved. +// + +#import +#import "DEWTableStaticSection.h" +#import "DEWTableDynamicSection.h" + +@interface DEWTableManager : NSObject + +@property (nonatomic, strong, readonly) NSArray > *sections; + +- (void)addSection:(id )section; +- (void)insertSection:(id )section atIndex:(NSInteger)index; +- (void)removeSectionAtIndex:(NSInteger)index; + +- (void)removeSection:(id )section; + +///如果不存在该section,返回-1 +- (NSInteger)indexForSection:(id )section; + +@end + + diff --git a/DEWTableManager.m b/DEWTableManager.m new file mode 100644 index 0000000..86e3ac8 --- /dev/null +++ b/DEWTableManager.m @@ -0,0 +1,153 @@ +// +// DEWTableManager.m +// DEWTableViewManager +// +// Created by Shaw on 2019/1/17. +// Copyright © 2019 Forcewith Co., Ltd. All rights reserved. +// + +#import "DEWTableManager.h" + +@interface DEWTableManager () + +@property (nonatomic, strong, readwrite) NSMutableArray > *mutableSections; + +@end + + +@implementation DEWTableManager + +- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView +{ + return self.sections.count; +} + +- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section +{ + id sectionModel = [self.sections objectAtIndex:section]; + return sectionModel.numberOfRows; +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath +{ + + id sectionModel = [self.sections objectAtIndex:indexPath.section]; + return [sectionModel tableView:tableView cellForRowAtIndexPath:indexPath]; +} + +- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath +{ + + id sectionModel = [self.sections objectAtIndex:indexPath.section]; + [sectionModel didSelectRowAtIndexPath:indexPath]; + [tableView deselectRowAtIndexPath:indexPath animated:YES]; +} + +- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath +{ + + id sectionModel = [self.sections objectAtIndex:indexPath.section]; + return [sectionModel heightForRowAtIndexPath:indexPath]; +} + +- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section +{ + id sectionModel = [self.sections objectAtIndex:section]; + return sectionModel.sectionHeader; +} + +- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section +{ + id sectionModel = [self.sections objectAtIndex:section]; + return sectionModel.sectionFooter; +} + +- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section +{ + id sectionModel = [self.sections objectAtIndex:section]; + return sectionModel.sectionHeader.bounds.size.height; +} + +- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section +{ + id sectionModel = [self.sections objectAtIndex:section]; + return sectionModel.sectionFooter.bounds.size.height; +} + +//Edit + +- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath +{ + + id sectionModel = [self.sections objectAtIndex:indexPath.section]; + return [sectionModel canEditRowAtIndexPath:indexPath]; +} + +- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath +{ + id sectionModel = [self.sections objectAtIndex:indexPath.section]; + return [sectionModel editingStyleForRowAtIndexPath:indexPath]; +} + +- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath +{ + + id sectionModel = [self.sections objectAtIndex:indexPath.section]; + [sectionModel commitEditingStyle:editingStyle forRowAtIndexPath:indexPath]; +} + +#pragma mark Public + +- (void)addSection:(id)section +{ + [self.mutableSections addObject:section]; +} + +- (void)insertSection:(id)section atIndex:(NSInteger)index; +{ + [self.mutableSections insertObject:section atIndex:index]; +} + +- (void)removeSectionAtIndex:(NSInteger)index +{ + [self.mutableSections removeObjectAtIndex:index]; +} + +- (void)removeSection:(id)section +{ + [self.mutableSections removeObject:section]; +} + +- (NSInteger)indexForSection:(id)section +{ + if (!section) { + return -1; + } + + __block NSInteger index = -1; + + [self.mutableSections enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + if ([section isEqual:obj]) { + index = idx; + *stop = YES; + return; + } + }]; + return index; +} + +#pragma mark Privite + +- (NSMutableArray> *)mutableSections +{ + if (_mutableSections) return _mutableSections; + _mutableSections = [[NSMutableArray alloc]init]; + return _mutableSections; +} + +- (NSArray > *)sections +{ + return self.mutableSections.copy; +} + +@end diff --git a/DEWTableSectionProtocol.h b/DEWTableSectionProtocol.h new file mode 100644 index 0000000..f2aff58 --- /dev/null +++ b/DEWTableSectionProtocol.h @@ -0,0 +1,30 @@ +// +// DEWTableSectionProtocol.h +// DEWTableViewManager +// +// Created by Shaw on 2019/1/17. +// Copyright © 2019 Forcewith Co., Ltd. All rights reserved. +// + +#import +#import + +@protocol DEWTableSectionProtocol + +@required + +@property (nonatomic, strong) UIView *sectionHeader; +@property (nonatomic, strong) UIView *sectionFooter; + +- (NSUInteger)numberOfRows; +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; +- (void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath; +- (CGFloat)heightForRowAtIndexPath:(NSIndexPath *)indexPath; +- (BOOL)canEditRowAtIndexPath:(NSIndexPath *)indexPath; +- (UITableViewCellEditingStyle)editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath; +- (void)commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath; + + +@end + + diff --git a/DEWTableStaticCellModel.h b/DEWTableStaticCellModel.h new file mode 100644 index 0000000..9e8915a --- /dev/null +++ b/DEWTableStaticCellModel.h @@ -0,0 +1,35 @@ +// +// DEWTableCellModel.h +// DEWTableViewManager +// +// Created by Shaw on 2019/1/17. +// Copyright © 2019 Forcewith Co., Ltd. All rights reserved. +// + +#import +#import + +typedef void(^DEWTableCellSelected)(NSIndexPath *indexPath); +typedef void(^DEWTableCellEditCommit)(NSIndexPath *indexPath, UITableViewCellEditingStyle editingStyle); + + +@interface DEWTableStaticCellModel : NSObject + +@property (nonatomic, strong) UITableViewCell *cell; + +@property (nonatomic, assign) CGFloat rowHeight; + +@property (nonatomic, assign) BOOL canEdit; +@property (nonatomic, assign) UITableViewCellEditingStyle editStyle; + +@property (nonatomic, copy) DEWTableCellSelected selected; +@property (nonatomic, copy) DEWTableCellEditCommit editCommit; + +- (instancetype)initWithCell:(UITableViewCell *)cell + rowHeight:(CGFloat)rowHeight + selected:(DEWTableCellSelected)selected; + +@end + + + diff --git a/DEWTableStaticCellModel.m b/DEWTableStaticCellModel.m new file mode 100644 index 0000000..0cfcfa3 --- /dev/null +++ b/DEWTableStaticCellModel.m @@ -0,0 +1,27 @@ +// +// DEWTableCellModel.m +// DEWTableViewManager +// +// Created by Shaw on 2019/1/17. +// Copyright © 2019 Forcewith Co., Ltd. All rights reserved. +// + +#import "DEWTableStaticCellModel.h" + +@implementation DEWTableStaticCellModel + +- (instancetype)initWithCell:(UITableViewCell *)cell + rowHeight:(CGFloat)rowHeight + selected:(DEWTableCellSelected)selected +{ + self = [super init]; + if (!self) return nil; + + self.cell = cell; + self.selected = [selected copy]; + self.rowHeight = rowHeight; + + return self; +} + +@end diff --git a/DEWTableStaticSection.h b/DEWTableStaticSection.h new file mode 100644 index 0000000..dd9dc0b --- /dev/null +++ b/DEWTableStaticSection.h @@ -0,0 +1,30 @@ +// +// DEWTableSection.h +// DEWTableViewManager +// +// Created by Shaw on 2019/1/17. +// Copyright © 2019 Forcewith Co., Ltd. All rights reserved. +// + +#import +#import "DEWTableSectionProtocol.h" +#import "DEWTableStaticCellModel.h" + +@interface DEWTableStaticSection : NSObject + +//protocol +@property (nonatomic, strong) UIView *sectionHeader; +@property (nonatomic, strong) UIView *sectionFooter; + +@property (nonatomic, assign) NSInteger numberOfRows; + +// +@property (nonatomic, strong, readonly) NSArray *cellModels; + +- (void)addCellModel:(DEWTableStaticCellModel *)cellModel; +- (void)insertCellModel:(DEWTableStaticCellModel *)cellModel atIndex:(NSInteger)index; +- (void)removeCellModelAtIndex:(NSInteger)index; + +@end + + diff --git a/DEWTableStaticSection.m b/DEWTableStaticSection.m new file mode 100644 index 0000000..3d14573 --- /dev/null +++ b/DEWTableStaticSection.m @@ -0,0 +1,97 @@ +// +// DEWTableSection.m +// DEWTableViewManager +// +// Created by Shaw on 2019/1/17. +// Copyright © 2019 Forcewith Co., Ltd. All rights reserved. +// + +#import "DEWTableStaticSection.h" + +@interface DEWTableStaticSection () + +@property (nonatomic, strong, readwrite) NSMutableArray *mutableCellModels; + +@end + + +@implementation DEWTableStaticSection + +- (void)addCellModel:(DEWTableStaticCellModel *)cellModel +{ + [self.mutableCellModels addObject:cellModel]; +} + +- (void)insertCellModel:(DEWTableStaticCellModel *)cellModel atIndex:(NSInteger)index +{ + [self.mutableCellModels insertObject:cellModel atIndex:index]; +} + +- (void)removeCellModelAtIndex:(NSInteger)index +{ + [self.mutableCellModels removeObjectAtIndex:index]; +} + +- (NSInteger)numberOfRows +{ + return self.cellModels.count; +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath +{ + + DEWTableStaticCellModel *cellModel = [self.cellModels objectAtIndex:indexPath.row]; + return cellModel.cell; +} + +- (void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath +{ + + DEWTableStaticCellModel *cellModel = [self.cellModels objectAtIndex:indexPath.row]; + if (cellModel.selected) { + cellModel.selected(indexPath); + } +} + +- (CGFloat)heightForRowAtIndexPath:(NSIndexPath *)indexPath +{ + DEWTableStaticCellModel *cellModel = [self.cellModels objectAtIndex:indexPath.row]; + return cellModel.rowHeight; +} + +- (BOOL)canEditRowAtIndexPath:(NSIndexPath *)indexPath +{ + DEWTableStaticCellModel *cellModel = [self.cellModels objectAtIndex:indexPath.row]; + return cellModel.canEdit; +} + +- (UITableViewCellEditingStyle)editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath +{ + DEWTableStaticCellModel *cellModel = [self.cellModels objectAtIndex:indexPath.row]; + return cellModel.editStyle; +} + +- (void)commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath +{ + DEWTableStaticCellModel *cellModel = [self.cellModels objectAtIndex:indexPath.row]; + if (cellModel.editCommit) { + cellModel.editCommit(indexPath, editingStyle); + } +} + +#pragma mark Getters + +- (NSMutableArray *)mutableCellModels +{ + if (_mutableCellModels) return _mutableCellModels; + _mutableCellModels = [[NSMutableArray alloc]init]; + return _mutableCellModels; +} + +- (NSArray *)cellModels +{ + return self.mutableCellModels.copy; +} + + +@end