Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
initial commit
  • Loading branch information
gongpengjun committed Nov 28, 2018
0 parents commit c3e7455
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 0 deletions.
15 changes: 15 additions & 0 deletions GPJDataDrivenTableView.podspec
@@ -0,0 +1,15 @@
Pod::Spec.new do |s|
s.name = "GPJDataDrivenTable"
s.version = "0.1"
s.summary = "A data-driven way to use UITableView."
s.description = "The data-drive way is simple, steady, extendable, It is friendly to evolve with change of requirements."
s.homepage = "https://github.com/gongpengjun/GPJDataDrivenTable"
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { "gongpengjun" => "frank.gongpengjun@gmail.com" }
s.platform = :ios
s.source = { :git => "https://github.com/gongpengjun/GPJDataDrivenTable.git", :tag => "0.1" }
s.source_files = 'GPJDataDrivenTable/**/*.{h,m}'
s.framework = "UIKit"
s.requires_arc = true
s.dependency 'Masonry'
end
32 changes: 32 additions & 0 deletions GPJDataDrivenTableView/GPJDataDrivenTableView.h
@@ -0,0 +1,32 @@
//
// GPJDataDrivenTableView.h
//
// Created by gongpengjun <frank.gongpengjun@gmail.com>
//

#import <UIKit/UIKit.h>

@class GPJDataDrivenTableView;

typedef void (^DGCBaseTableConfigTableViewBlock)(GPJDataDrivenTableView *tableContainerView, UITableView *tableView);

@interface GPJBaseData : NSObject
@property (nonatomic, copy) void (^didSelectAction)(id data);
- (CGFloat)cellHeight;
@end

@interface GPJBaseCell : UITableViewCell
@property (nonatomic, strong) id data;
- (void)configCell;
@end

@interface GPJDataDrivenTableView : UIView <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArray;

- (instancetype)initWithFrame:(CGRect)frame;
- (instancetype)initWithFrame:(CGRect)frame configTableViewBlock:(DGCBaseTableConfigTableViewBlock)configTableViewBlock;
- (void)reloadData;

@end
193 changes: 193 additions & 0 deletions GPJDataDrivenTableView/GPJDataDrivenTableView.m
@@ -0,0 +1,193 @@
//
// GPJDataDrivenTableView.m
//
// Created by gongpengjun <frank.gongpengjun@gmail.com>
//

#import "GPJDataDrivenTableView.h"
#import <Masonry/Masonry.h>

#define kDefaultCellHeight 44.0f

@implementation GPJBaseData

@synthesize didSelectAction;

- (CGFloat)cellHeight;
{
return kDefaultCellHeight;
}

@end

@interface GPJBaseCell ()
+ (NSString *)GPJReuseIdentifier; // cell reuse identifier
@end

@implementation GPJBaseCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self) {
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.clipsToBounds = YES;
}
return self;
}

+ (NSString *)GPJReuseIdentifier;
{
return NSStringFromClass([self class]);
}

- (void)setData:(id)data
{
_data = data;
[self configCell];
}

- (void)configCell;
{
// do nothing
}

@end

@implementation GPJDataDrivenTableView

#pragma mark - Life Cycle

- (void)dealloc;
{
_tableView.delegate = nil;
_tableView.dataSource = nil;
}

- (instancetype)initWithFrame:(CGRect)frame;
{
return [self initWithFrame:frame configTableViewBlock:nil];
}

- (instancetype)initWithFrame:(CGRect)frame configTableViewBlock:(DGCBaseTableConfigTableViewBlock)configTableViewBlock;
{
self = [super initWithFrame:frame];
if(self) {
self.tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
[self insertSubview:self.tableView atIndex:0];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
if (configTableViewBlock) {
configTableViewBlock(self, self.tableView);
}
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
return self;
}

- (void)reloadData;
{
[self.tableView reloadData];
}

#pragma mark - Data to Cell Mapping

- (NSInteger)sectionCount
{
return 1;
}

- (NSInteger)rowCountInSection:(NSInteger)section;
{
return self.dataArray.count;
}

- (id)dataForIndexPath:(NSIndexPath *)indexPath;
{
if(indexPath.row < self.dataArray.count)
return self.dataArray[indexPath.row];
else
return nil;
}

- (Class)cellClassForIndexPath:(NSIndexPath *)indexPath;
{
id data = [self dataForIndexPath:indexPath];
Class cellClass = [self cellClassForDataClass:[data class]];
return cellClass;
}

- (NSString *)reuseIdentifierForIndexPath:(NSIndexPath *)indexPath;
{
return [[self cellClassForIndexPath:indexPath] GPJReuseIdentifier];
}

- (CGFloat)heightForIndexPath:(NSIndexPath *)indexPath;
{
id data = [self dataForIndexPath:indexPath];
if ([data isKindOfClass:[GPJBaseData class]]) {
return [data cellHeight];
} else {
return kDefaultCellHeight;
}
}

#pragma mark - Data -> Cell name mapping

- (Class)cellClassForDataClass:(Class)dataClass;
{
NSString *dataClassName = NSStringFromClass(dataClass);
NSString *cellClassName = nil;
if ([dataClassName hasSuffix:@"Data"]) {
cellClassName = [[dataClassName substringToIndex:dataClassName.length-@"Data".length] stringByAppendingString:@"Cell"];
}
Class cellClass = NSClassFromString(cellClassName);
NSAssert(cellClass, @"fatal error: NO cell class '%@' for data class '%@'",cellClassName,dataClassName);
return cellClass;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self sectionCount];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self rowCountInSection:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *reuseIdentifier = [self reuseIdentifierForIndexPath:indexPath];
Class cellClass = [self cellClassForIndexPath:indexPath];
GPJBaseCell *cell = [_tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
cell.data = [self dataForIndexPath:indexPath];
return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return [self heightForIndexPath:indexPath];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
id data = [self dataForIndexPath:indexPath];
if (![data isKindOfClass:[GPJBaseData class]])
return;
GPJBaseData *baseData = (GPJBaseData *)data;
if (baseData.didSelectAction) {
baseData.didSelectAction(data);
}
}

@end
19 changes: 19 additions & 0 deletions LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2017 Gong Pengjun <frank.gongpengjun@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
36 changes: 36 additions & 0 deletions README.md
@@ -0,0 +1,36 @@
# GPJDataDrivenTableView

## Description

__GPJDataDrivenTableView__ is a data-driven way to use UITableView. It is easy to use. It is friendly to evolve with change of requirements.

## Installation

### CocoaPods

```ruby
pod 'GPJDataDrivenTable'
```

### Manual

1. download the GPJDataDrivenTableView repository
2. copy the GPJDataDrivenTableView sub-folder into your Xcode project

## Origin

The traditional way is index-driven, we implement the UITableViewDataSource's or UITableViewDelegate's methods base on __indexPath__:

- `-tableView:cellForRowAtIndexPath:`
- `-tableView:heightForRowAtIndexPath:`
- `-tableView:didSelectRowAtIndexPath:`

This index-driven way based on __indexPath__ results a lot of `if-else` code segments, Various cells' code mix together. it is error-prone, hard to evolve with change of requirements.

![](docs/UITableView_IndexDriven.png)

The core role of data-driven way to use UITableView is GPJDataDrivenTableView class. GPJDataDrivenTableView contains UITableView instance and a dataArray, GPJDataDrivenTableView set itself as UITableView's dataSource and delegate, implement the protocol methods, and covert ___indexPath___ to ___data___ in dataArray. Finally we can render cell ui and respond to row selection based on ___data___.

![](docs/UITableView_DataDriven.png)

This data-driven way is simple, easy to implement each kind of data, cell, and select action separately.
Binary file added docs/UITableView_DataDriven.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/UITableView_IndexDriven.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c3e7455

Please sign in to comment.