Skip to content

Commit 2c82fb7

Browse files
committed
增加不同cell高度的支持
1 parent c9ac73c commit 2c82fb7

18 files changed

+223
-167
lines changed

README.md

+23-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
![](https://img.shields.io/cocoapods/v/{Somo}.svg?style=flat)
44
![](https://img.shields.io/badge/platform-ios-lightgrey.svg)
55
![](https://img.shields.io/badge/language-objc-orange.svg)
6-
7-
#### <a id="somo_intro"></a>简介
8-
6+
97
- [x] iOS 7.0+
108
- [x] 多样式
119
- [x] 轻量级,核心实现仅仅是对UIView进行扩展
1210
- [x] 可以自定义
11+
- [x] 支持不同高度的cell
1312

1413
<table>
1514
<tr>
@@ -81,14 +80,34 @@ pod 'Somo'
8180
的SomoDataSourceProvider实例。当数据着陆后,将tableview的datasource和delegate指向controller或其他。
8281

8382
* 数据着陆前:
83+
8484
```objective-c
85-
#pragma mark - provider
85+
8686
//将tableview的datasource指向SomoDataSourceProvider
8787
//当数据加载完成后,将tableview的datasource指向self
88+
//cell高度相同
8889
self.provider = [SomoDataSourceProvider dataSourceProviderWithCellReuseIdentifier:@"id"];
8990
self.tableView.dataSource = self.provider;
9091
self.tableView.delegate = self.provider;
9192
```
93+
94+
//cell高度不同
95+
96+
```objective-c
97+
self.provider = [[SomoDataSourceProvider alloc] initWithTableViewCellBlock:^UITableViewCell<SomoSkeletonLayoutProtocol> *(UITableView *tableView, NSIndexPath *indexPath) {
98+
if(indexPath.row%2 == 0){
99+
return [tableView dequeueReusableCellWithIdentifier:@"id" forIndexPath:indexPath];
100+
}else{
101+
return [tableView dequeueReusableCellWithIdentifier:@"oid" forIndexPath:indexPath];
102+
}
103+
} heightBlock:^CGFloat(UITableView *tableview, NSIndexPath *indexPath) {
104+
if(indexPath.row%2 == 0){
105+
return 120;
106+
}else{
107+
return 80;
108+
}
109+
}];
110+
```
92111
* 数据着陆后:
93112
```objective-c
94113
#pragma mark -

Somo.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Pod::Spec.new do |s|
33

44
s.name = "Somo"
5-
s.version = "0.7.7"
5+
s.version = "0.8.0"
66
s.summary = "Somo is a Skeleton-style animation library that's simple enough."
77

88
s.homepage = "https://github.com/xorshine/Somo"

SomoDemo/SomoDemo.xcodeproj/project.pbxproj

+26-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
983656721FD79CA7008F57D4 /* DataSourceTableViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 983656701FD79CA7008F57D4 /* DataSourceTableViewController.xib */; };
1313
983656761FD7A519008F57D4 /* DataSourceTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 983656741FD7A519008F57D4 /* DataSourceTableViewCell.m */; };
1414
983656771FD7A519008F57D4 /* DataSourceTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 983656751FD7A519008F57D4 /* DataSourceTableViewCell.xib */; };
15+
9863805E20664BCD00D44555 /* OtherTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 9863805C20664BCD00D44555 /* OtherTableViewCell.m */; };
16+
9863805F20664BCD00D44555 /* OtherTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9863805D20664BCD00D44555 /* OtherTableViewCell.xib */; };
1517
98F5EADA1FC6A74C00D91FA1 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 98F5EAD91FC6A74C00D91FA1 /* AppDelegate.m */; };
1618
98F5EAE01FC6A74C00D91FA1 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 98F5EADE1FC6A74C00D91FA1 /* Main.storyboard */; };
1719
98F5EAE21FC6A74C00D91FA1 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 98F5EAE11FC6A74C00D91FA1 /* Assets.xcassets */; };
@@ -38,6 +40,9 @@
3840
983656731FD7A519008F57D4 /* DataSourceTableViewCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DataSourceTableViewCell.h; sourceTree = "<group>"; };
3941
983656741FD7A519008F57D4 /* DataSourceTableViewCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DataSourceTableViewCell.m; sourceTree = "<group>"; };
4042
983656751FD7A519008F57D4 /* DataSourceTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = DataSourceTableViewCell.xib; sourceTree = "<group>"; };
43+
9863805B20664BCD00D44555 /* OtherTableViewCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OtherTableViewCell.h; sourceTree = "<group>"; };
44+
9863805C20664BCD00D44555 /* OtherTableViewCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OtherTableViewCell.m; sourceTree = "<group>"; };
45+
9863805D20664BCD00D44555 /* OtherTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = OtherTableViewCell.xib; sourceTree = "<group>"; };
4146
98D3E36A1FC95EEA007F5A97 /* SomoDefines.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SomoDefines.h; sourceTree = "<group>"; };
4247
98F5EAD51FC6A74C00D91FA1 /* SomoDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SomoDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
4348
98F5EAD81FC6A74C00D91FA1 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
@@ -80,6 +85,24 @@
8085
/* End PBXFrameworksBuildPhase section */
8186

8287
/* Begin PBXGroup section */
88+
9863806020664C3C00D44555 /* Views */ = {
89+
isa = PBXGroup;
90+
children = (
91+
98F5EAF71FC8148B00D91FA1 /* SView.h */,
92+
98F5EAF81FC8148B00D91FA1 /* SView.m */,
93+
98F5EB021FC91EE600D91FA1 /* TableViewCell.h */,
94+
98F5EB031FC91EE600D91FA1 /* TableViewCell.m */,
95+
98F5EB041FC91EE600D91FA1 /* TableViewCell.xib */,
96+
9863805B20664BCD00D44555 /* OtherTableViewCell.h */,
97+
9863805C20664BCD00D44555 /* OtherTableViewCell.m */,
98+
9863805D20664BCD00D44555 /* OtherTableViewCell.xib */,
99+
983656731FD7A519008F57D4 /* DataSourceTableViewCell.h */,
100+
983656741FD7A519008F57D4 /* DataSourceTableViewCell.m */,
101+
983656751FD7A519008F57D4 /* DataSourceTableViewCell.xib */,
102+
);
103+
path = Views;
104+
sourceTree = "<group>";
105+
};
83106
98F5EACC1FC6A74C00D91FA1 = {
84107
isa = PBXGroup;
85108
children = (
@@ -113,14 +136,7 @@
113136
9836566E1FD79CA7008F57D4 /* DataSourceTableViewController.h */,
114137
9836566F1FD79CA7008F57D4 /* DataSourceTableViewController.m */,
115138
983656701FD79CA7008F57D4 /* DataSourceTableViewController.xib */,
116-
98F5EB021FC91EE600D91FA1 /* TableViewCell.h */,
117-
98F5EB031FC91EE600D91FA1 /* TableViewCell.m */,
118-
98F5EB041FC91EE600D91FA1 /* TableViewCell.xib */,
119-
983656731FD7A519008F57D4 /* DataSourceTableViewCell.h */,
120-
983656741FD7A519008F57D4 /* DataSourceTableViewCell.m */,
121-
983656751FD7A519008F57D4 /* DataSourceTableViewCell.xib */,
122-
98F5EAF71FC8148B00D91FA1 /* SView.h */,
123-
98F5EAF81FC8148B00D91FA1 /* SView.m */,
139+
9863806020664C3C00D44555 /* Views */,
124140
98F5EADE1FC6A74C00D91FA1 /* Main.storyboard */,
125141
98F5EAE11FC6A74C00D91FA1 /* Assets.xcassets */,
126142
98F5EAE31FC6A74C00D91FA1 /* LaunchScreen.storyboard */,
@@ -211,6 +227,7 @@
211227
98F5EAE51FC6A74C00D91FA1 /* LaunchScreen.storyboard in Resources */,
212228
983656721FD79CA7008F57D4 /* DataSourceTableViewController.xib in Resources */,
213229
98F5EAE21FC6A74C00D91FA1 /* Assets.xcassets in Resources */,
230+
9863805F20664BCD00D44555 /* OtherTableViewCell.xib in Resources */,
214231
98F5EAE01FC6A74C00D91FA1 /* Main.storyboard in Resources */,
215232
98F5EB0D1FC9213D00D91FA1 /* CommonViewController.xib in Resources */,
216233
);
@@ -230,6 +247,7 @@
230247
98F5EAF11FC6B69D00D91FA1 /* UIView+SomoSkeleton.m in Sources */,
231248
9836566D1FD798CF008F57D4 /* SomoDataSourceProvider.m in Sources */,
232249
983656761FD7A519008F57D4 /* DataSourceTableViewCell.m in Sources */,
250+
9863805E20664BCD00D44555 /* OtherTableViewCell.m in Sources */,
233251
98F5EB051FC91EE600D91FA1 /* TableViewCell.m in Sources */,
234252
98F5EADA1FC6A74C00D91FA1 /* AppDelegate.m in Sources */,
235253
98F5EAF51FC7F92D00D91FA1 /* SomoView.m in Sources */,

SomoDemo/SomoDemo/DataSourceTableViewController.m

+31-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import "DataSourceTableViewController.h"
1010
#import "DataSourceTableViewCell.h"
11+
#import "OtherTableViewCell.h"
1112
#import "Somo.h"
1213

1314
@interface DataSourceTableViewController ()
@@ -22,17 +23,29 @@ @implementation DataSourceTableViewController
2223

2324
- (void)viewDidLoad {
2425
[super viewDidLoad];
25-
26-
self.tableView.rowHeight = 100;
26+
2727
[self.tableView registerNib:[UINib nibWithNibName:@"DataSourceTableViewCell" bundle:nil] forCellReuseIdentifier:@"id"];
28+
[self.tableView registerNib:[UINib nibWithNibName:@"OtherTableViewCell" bundle:nil] forCellReuseIdentifier:@"oid"];
2829

2930
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Logo"]];
3031
self.navigationItem.titleView.contentMode = UIViewContentModeScaleAspectFit;
3132

3233
#pragma mark - provider
3334
// 将tableview的datasource指向SomoDataSourceProvider
3435
// 当数据加载完成后,将tableview的datasource指向self
35-
self.provider = [SomoDataSourceProvider dataSourceProviderWithCellReuseIdentifier:@"id"];
36+
self.provider = [[SomoDataSourceProvider alloc] initWithTableViewCellBlock:^UITableViewCell<SomoSkeletonLayoutProtocol> *(UITableView *tableView, NSIndexPath *indexPath) {
37+
if(indexPath.row%2 == 0){
38+
return [tableView dequeueReusableCellWithIdentifier:@"id" forIndexPath:indexPath];
39+
}else{
40+
return [tableView dequeueReusableCellWithIdentifier:@"oid" forIndexPath:indexPath];
41+
}
42+
} heightBlock:^CGFloat(UITableView *tableview, NSIndexPath *indexPath) {
43+
if(indexPath.row%2 == 0){
44+
return 120;
45+
}else{
46+
return 80;
47+
}
48+
}];
3649
self.tableView.dataSource = self.provider;
3750
self.tableView.delegate = self.provider;
3851
}
@@ -60,9 +73,9 @@ - (void)load{
6073
#pragma mark -
6174
//==================================================
6275
self.tableView.dataSource = self;
63-
self.tableView.delegate = self;
64-
//==================================================
76+
self.tableView.delegate = self;
6577
[self.tableView reloadData];
78+
//==================================================
6679
}
6780

6881
#pragma mark - Table view data source
@@ -71,9 +84,20 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
7184
return self.dataSource.count;
7285
}
7386

87+
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
88+
if(indexPath.row%2 == 0){
89+
return 120;
90+
}else{
91+
return 80;
92+
}
93+
}
94+
7495
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
75-
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"id" forIndexPath:indexPath];
76-
return cell;
96+
if(indexPath.row%2 == 0){
97+
return [tableView dequeueReusableCellWithIdentifier:@"id" forIndexPath:indexPath];
98+
}else{
99+
return [tableView dequeueReusableCellWithIdentifier:@"oid" forIndexPath:indexPath];
100+
}
77101
}
78102

79103
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

SomoDemo/SomoDemo/Somo/SomoDataSourceProvider.h

+8-38
Original file line numberDiff line numberDiff line change
@@ -13,56 +13,26 @@
1313
@protocol SomoSkeletonLayoutProtocol;
1414

1515
typedef UITableViewCell<SomoSkeletonLayoutProtocol> *(^SomoTableViewCellBlock)(UITableView *tableview, NSIndexPath *indexPath);
16-
typedef UICollectionViewCell<SomoSkeletonLayoutProtocol> *(^SomoCollectionViewCellBlock)(UICollectionView *collectionView, NSIndexPath *indexPath);
17-
18-
typedef NSInteger(^SomoTableViewNumberOfSectionsBlock)(UITableView *tableview, NSIndexPath *indexPath);
19-
typedef CGFloat(^SomoTableViewCellHeightBlock)(UITableView *tableview);
20-
typedef NSInteger(^SomoTableViewNumberOfRowsInSectionsSectionsBlock)(UITableView *tableview,NSInteger section);
21-
22-
//typedef NSInteger(^SomoCollectionViewNumberOfSectionBlock)(UICollectionView *collectionView);
23-
//
24-
//typedef NSInteger(^SomoCollectionViewNumberOfRowInsSections)(UICollectionView *collectionView,NSInteger section);
25-
//
26-
//typedef CGSize(^SomoCollectionViewLayoutSize)(UICollectionView *collectionView,UICollectionViewLayout*collectionViewLayout,NSIndexPath *indexPath);
27-
//
28-
//typedef UIEdgeInsets(^SomoCollectionViewLayoutEdgeInsets)(UICollectionView *collectionView,UICollectionViewLayout*collectionViewLayout,NSInteger section);
29-
//
30-
//typedef CGFloat(^SomoCollectionViewLayoutMinimumLineSpacing)(UICollectionView *collectionView,UICollectionViewLayout*collectionViewLayout,NSInteger section);
31-
//
32-
//typedef CGFloat(^SomoCollectionViewLayoutMinimumInteritemSpacing)(UICollectionView *collectionView,UICollectionViewLayout*collectionViewLayout,NSInteger section);
33-
//
34-
//typedef CGSize(^SomoCollectionViewLayoutReferenceSizeForHeader)(UICollectionView *collectionView,UICollectionViewLayout*collectionViewLayout,NSInteger section);
35-
//
36-
//typedef CGSize(^SomoCollectionViewLayoutReferenceSizeForFooter)(UICollectionView *collectionView,UICollectionViewLayout*collectionViewLayout,NSInteger section);
37-
38-
16+
typedef CGFloat(^SomoTableViewCellHeightBlock)(UITableView *tableView,NSIndexPath *indexPath);
17+
3918
@interface SomoDataSourceProvider : NSObject<UITableViewDataSource,UITableViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
4019

4120
/**
42-
default 15
21+
default 20
4322
*/
4423
@property (assign, nonatomic) NSInteger numberOfRowsInSection;
4524

4625
/**
47-
If you have only one cell style, use the following method
26+
If you have only one cell style, using the following methods
4827
*/
4928
- (instancetype)initWithCellReuseIdentifier:(NSString *)reuseIdentifier;
5029
+ (instancetype)dataSourceProviderWithCellReuseIdentifier:(NSString *)reuseIdentifier;
5130

5231
/**
53-
If you have different cell styles, use the following method
32+
If you use different cell styles, using the following methods
5433
*/
55-
//- (instancetype)initWithTableViewCellBlock:(SomoTableViewCellBlock)block
56-
// heightBlock:(SomoTableViewCellHeightBlock)heightBlock;
57-
//
58-
//- (instancetype)initWithCollectionViewCellBlock:(SomoCollectionViewCellBlock)block
59-
// sections:(SomoCollectionViewNumberOfSectionBlock)sections
60-
// rows:(SomoCollectionViewNumberOfRowInsSections)rows
61-
// size:(SomoCollectionViewLayoutSize)size
62-
// edgeInsets:(SomoCollectionViewLayoutEdgeInsets)edgeInsets
63-
// minimumLineSpacing:(SomoCollectionViewLayoutMinimumLineSpacing)minimumLineSpacing
64-
// minimumInteritemSpacing:(SomoCollectionViewLayoutMinimumInteritemSpacing)minimumInteritemSpacing
65-
// referenceSizeForHeader:(SomoCollectionViewLayoutReferenceSizeForHeader)referenceSizeForHeader
66-
// referenceSizeForFooter:(SomoCollectionViewLayoutReferenceSizeForFooter)referenceSizeForFooter;
34+
- (instancetype)initWithTableViewCellBlock:(SomoTableViewCellBlock)block
35+
heightBlock:(SomoTableViewCellHeightBlock)heightBlock;
36+
6737

6838
@end

0 commit comments

Comments
 (0)