-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
545 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// DEWTableDynamicSection.h | ||
// DEWTableViewManager | ||
// | ||
// Created by Shaw on 2019/1/17. | ||
// Copyright © 2019 Forcewith Co., Ltd. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#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<DEWTableSectionProtocol> | ||
|
||
//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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// DEWTableManager.h | ||
// DEWTableViewManager | ||
// | ||
// Created by Shaw on 2019/1/17. | ||
// Copyright © 2019 Forcewith Co., Ltd. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "DEWTableStaticSection.h" | ||
#import "DEWTableDynamicSection.h" | ||
|
||
@interface DEWTableManager : NSObject<UITableViewDelegate, UITableViewDataSource> | ||
|
||
@property (nonatomic, strong, readonly) NSArray <id <DEWTableSectionProtocol>> *sections; | ||
|
||
- (void)addSection:(id <DEWTableSectionProtocol>)section; | ||
- (void)insertSection:(id <DEWTableSectionProtocol>)section atIndex:(NSInteger)index; | ||
- (void)removeSectionAtIndex:(NSInteger)index; | ||
|
||
- (void)removeSection:(id <DEWTableSectionProtocol>)section; | ||
|
||
///如果不存在该section,返回-1 | ||
- (NSInteger)indexForSection:(id <DEWTableSectionProtocol>)section; | ||
|
||
@end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <id <DEWTableSectionProtocol>> *mutableSections; | ||
|
||
@end | ||
|
||
|
||
@implementation DEWTableManager | ||
|
||
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView | ||
{ | ||
return self.sections.count; | ||
} | ||
|
||
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | ||
{ | ||
id<DEWTableSectionProtocol> sectionModel = [self.sections objectAtIndex:section]; | ||
return sectionModel.numberOfRows; | ||
} | ||
|
||
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | ||
{ | ||
|
||
id<DEWTableSectionProtocol> sectionModel = [self.sections objectAtIndex:indexPath.section]; | ||
return [sectionModel tableView:tableView cellForRowAtIndexPath:indexPath]; | ||
} | ||
|
||
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath | ||
{ | ||
|
||
id<DEWTableSectionProtocol> sectionModel = [self.sections objectAtIndex:indexPath.section]; | ||
[sectionModel didSelectRowAtIndexPath:indexPath]; | ||
[tableView deselectRowAtIndexPath:indexPath animated:YES]; | ||
} | ||
|
||
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath | ||
{ | ||
|
||
id<DEWTableSectionProtocol> sectionModel = [self.sections objectAtIndex:indexPath.section]; | ||
return [sectionModel heightForRowAtIndexPath:indexPath]; | ||
} | ||
|
||
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section | ||
{ | ||
id<DEWTableSectionProtocol> sectionModel = [self.sections objectAtIndex:section]; | ||
return sectionModel.sectionHeader; | ||
} | ||
|
||
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section | ||
{ | ||
id<DEWTableSectionProtocol> sectionModel = [self.sections objectAtIndex:section]; | ||
return sectionModel.sectionFooter; | ||
} | ||
|
||
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section | ||
{ | ||
id<DEWTableSectionProtocol> sectionModel = [self.sections objectAtIndex:section]; | ||
return sectionModel.sectionHeader.bounds.size.height; | ||
} | ||
|
||
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section | ||
{ | ||
id<DEWTableSectionProtocol> sectionModel = [self.sections objectAtIndex:section]; | ||
return sectionModel.sectionFooter.bounds.size.height; | ||
} | ||
|
||
//Edit | ||
|
||
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath | ||
{ | ||
|
||
id<DEWTableSectionProtocol> sectionModel = [self.sections objectAtIndex:indexPath.section]; | ||
return [sectionModel canEditRowAtIndexPath:indexPath]; | ||
} | ||
|
||
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath | ||
{ | ||
id<DEWTableSectionProtocol> sectionModel = [self.sections objectAtIndex:indexPath.section]; | ||
return [sectionModel editingStyleForRowAtIndexPath:indexPath]; | ||
} | ||
|
||
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath | ||
{ | ||
|
||
id<DEWTableSectionProtocol> sectionModel = [self.sections objectAtIndex:indexPath.section]; | ||
[sectionModel commitEditingStyle:editingStyle forRowAtIndexPath:indexPath]; | ||
} | ||
|
||
#pragma mark Public | ||
|
||
- (void)addSection:(id<DEWTableSectionProtocol>)section | ||
{ | ||
[self.mutableSections addObject:section]; | ||
} | ||
|
||
- (void)insertSection:(id<DEWTableSectionProtocol>)section atIndex:(NSInteger)index; | ||
{ | ||
[self.mutableSections insertObject:section atIndex:index]; | ||
} | ||
|
||
- (void)removeSectionAtIndex:(NSInteger)index | ||
{ | ||
[self.mutableSections removeObjectAtIndex:index]; | ||
} | ||
|
||
- (void)removeSection:(id<DEWTableSectionProtocol>)section | ||
{ | ||
[self.mutableSections removeObject:section]; | ||
} | ||
|
||
- (NSInteger)indexForSection:(id<DEWTableSectionProtocol>)section | ||
{ | ||
if (!section) { | ||
return -1; | ||
} | ||
|
||
__block NSInteger index = -1; | ||
|
||
[self.mutableSections enumerateObjectsUsingBlock:^(id<DEWTableSectionProtocol> _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { | ||
if ([section isEqual:obj]) { | ||
index = idx; | ||
*stop = YES; | ||
return; | ||
} | ||
}]; | ||
return index; | ||
} | ||
|
||
#pragma mark Privite | ||
|
||
- (NSMutableArray<id<DEWTableSectionProtocol>> *)mutableSections | ||
{ | ||
if (_mutableSections) return _mutableSections; | ||
_mutableSections = [[NSMutableArray alloc]init]; | ||
return _mutableSections; | ||
} | ||
|
||
- (NSArray <id<DEWTableSectionProtocol>> *)sections | ||
{ | ||
return self.mutableSections.copy; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// DEWTableSectionProtocol.h | ||
// DEWTableViewManager | ||
// | ||
// Created by Shaw on 2019/1/17. | ||
// Copyright © 2019 Forcewith Co., Ltd. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <UIKit/UIKit.h> | ||
|
||
@protocol DEWTableSectionProtocol <NSObject> | ||
|
||
@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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// DEWTableCellModel.h | ||
// DEWTableViewManager | ||
// | ||
// Created by Shaw on 2019/1/17. | ||
// Copyright © 2019 Forcewith Co., Ltd. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <UIKit/UIKit.h> | ||
|
||
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 | ||
|
||
|
||
|
Oops, something went wrong.