Skip to content

Commit

Permalink
优化 API 及数据结构。
Browse files Browse the repository at this point in the history
  • Loading branch information
liangdahong committed Jan 15, 2021
1 parent 606043f commit 8a8bb6f
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
#import "BMLongPressDragCellCollectionViewDataSource.h"

/*
- (NSArray *)dataSourceWithDragCellCollectionView:(BMLongPressDragCellCollectionView *)dragCellCollectionView {
- (NSArray<NSArray<id> *> *)dataSourceWithDragCellCollectionView:(__kindof BMLongPressDragCellCollectionView *)dragCellCollectionView {
return self.dataSourceArray;
// 此协议方法里返回数据源
}
- (void)dragCellCollectionView:(BMLongPressDragCellCollectionView *)dragCellCollectionView newDataArrayAfterMove:(nullable NSArray *)newDataArray {
- (void)dragCellCollectionView:(__kindof BMLongPressDragCellCollectionView *)dragCellCollectionView newDataArrayAfterMove:(nullable NSArray <NSArray <id> *> *)newDataArray {
self.dataSourceArray = [newDataArray mutableCopy];
// 此协议方法里保存数据源
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ NS_ASSUME_NONNULL_BEGIN
return self.dataArray;
}
*/
- (nullable NSArray *)dataSourceWithDragCellCollectionView:(__kindof BMLongPressDragCellCollectionView *)dragCellCollectionView;
- (nullable NSArray <NSArray <id> *> *)dataSourceWithDragCellCollectionView:(__kindof BMLongPressDragCellCollectionView *)dragCellCollectionView;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ NS_ASSUME_NONNULL_BEGIN
@param dragCellCollectionView dragCellCollectionView
@param newDataArray 最新的数据源,需要使用者保存,在拖拽的时候 Cell 已经移动,需保存最新的数据源
- (void)dragCellCollectionView:(BMLongPressDragCellCollectionView *)dragCellCollectionView newDataArrayAfterMove:(nullable NSArray *)newDataArray {
- (void)dragCellCollectionView:(__kindof BMLongPressDragCellCollectionView *)dragCellCollectionView newDataArrayAfterMove:(nullable NSArray <NSArray <id> *> *)newDataArray {
self.dataArray = [newDataArray copy];
}
*/
- (void)dragCellCollectionView:(__kindof BMLongPressDragCellCollectionView *)dragCellCollectionView newDataArrayAfterMove:(nullable NSArray *)newDataArray;
- (void)dragCellCollectionView:(__kindof BMLongPressDragCellCollectionView *)dragCellCollectionView newDataArrayAfterMove:(nullable NSArray <NSArray <id> *> *)newDataArray;

@optional

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
056CBE6B24AC5D8300FAD464 /* SwiftDemoVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 056CBE6924AC5D8300FAD464 /* SwiftDemoVC.swift */; };
056CBE6C24AC5D8300FAD464 /* SwiftDemoVC.xib in Resources */ = {isa = PBXBuildFile; fileRef = 056CBE6A24AC5D8300FAD464 /* SwiftDemoVC.xib */; };
056CBE6F24AC5FC000FAD464 /* SwiftDemoCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 056CBE6D24AC5FC000FAD464 /* SwiftDemoCollectionViewCell.swift */; };
059F341824AC705200634413 /* SwiftDemoXibCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 059F341624AC705200634413 /* SwiftDemoXibCollectionViewCell.swift */; };
059F341924AC705200634413 /* SwiftDemoXibCollectionViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 059F341724AC705200634413 /* SwiftDemoXibCollectionViewCell.xib */; };
05C4FEBD24A1EB0000260E73 /* BMChannelEditVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 05C4FEA624A1EB0000260E73 /* BMChannelEditVC.m */; };
05C4FEBE24A1EB0000260E73 /* BMChannelEditVC.xib in Resources */ = {isa = PBXBuildFile; fileRef = 05C4FEA724A1EB0000260E73 /* BMChannelEditVC.xib */; };
05C4FEBF24A1EB0000260E73 /* BMChannelModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 05C4FEAA24A1EB0000260E73 /* BMChannelModel.m */; };
Expand Down Expand Up @@ -123,8 +121,6 @@
056CBE6924AC5D8300FAD464 /* SwiftDemoVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftDemoVC.swift; sourceTree = "<group>"; };
056CBE6A24AC5D8300FAD464 /* SwiftDemoVC.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = SwiftDemoVC.xib; sourceTree = "<group>"; };
056CBE6D24AC5FC000FAD464 /* SwiftDemoCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftDemoCollectionViewCell.swift; sourceTree = "<group>"; };
059F341624AC705200634413 /* SwiftDemoXibCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftDemoXibCollectionViewCell.swift; sourceTree = "<group>"; };
059F341724AC705200634413 /* SwiftDemoXibCollectionViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = SwiftDemoXibCollectionViewCell.xib; sourceTree = "<group>"; };
05C4FEA524A1EB0000260E73 /* BMChannelEditVC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BMChannelEditVC.h; sourceTree = "<group>"; };
05C4FEA624A1EB0000260E73 /* BMChannelEditVC.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BMChannelEditVC.m; sourceTree = "<group>"; };
05C4FEA724A1EB0000260E73 /* BMChannelEditVC.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = BMChannelEditVC.xib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -456,8 +452,6 @@
056CBE6924AC5D8300FAD464 /* SwiftDemoVC.swift */,
056CBE6A24AC5D8300FAD464 /* SwiftDemoVC.xib */,
056CBE6D24AC5FC000FAD464 /* SwiftDemoCollectionViewCell.swift */,
059F341624AC705200634413 /* SwiftDemoXibCollectionViewCell.swift */,
059F341724AC705200634413 /* SwiftDemoXibCollectionViewCell.xib */,
);
path = Swift;
sourceTree = "<group>";
Expand Down Expand Up @@ -754,7 +748,6 @@
05C4FEBE24A1EB0000260E73 /* BMChannelEditVC.xib in Resources */,
0529D8042493795400EA22B9 /* BMAlipay2VC.xib in Resources */,
056CBE6C24AC5D8300FAD464 /* SwiftDemoVC.xib in Resources */,
059F341924AC705200634413 /* SwiftDemoXibCollectionViewCell.xib in Resources */,
6003F5A9195388D20070C39A /* Images.xcassets in Resources */,
6003F598195388D20070C39A /* InfoPlist.strings in Resources */,
0529D8092493795400EA22B9 /* BMAlipay2HeaderView.xib in Resources */,
Expand Down Expand Up @@ -858,7 +851,6 @@
0529D7F92493795400EA22B9 /* BMModel.m in Sources */,
056CBE6B24AC5D8300FAD464 /* SwiftDemoVC.swift in Sources */,
0529D8122493795400EA22B9 /* BMImageCell.m in Sources */,
059F341824AC705200634413 /* SwiftDemoXibCollectionViewCell.swift in Sources */,
0529D7FC2493795400EA22B9 /* BMTestSizeVC.m in Sources */,
0529D8CB2493795400EA22B9 /* main.m in Sources */,
05C4FEC024A1EB0000260E73 /* BMChannelEditCell.m in Sources */,
Expand Down Expand Up @@ -935,6 +927,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand All @@ -959,7 +952,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
Expand Down Expand Up @@ -989,6 +982,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand All @@ -1006,8 +1000,9 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.3;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
SDKROOT = iphoneos;
SWIFT_COMPILATION_MODE = wholemodule;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ @interface BMAlipayVC () <BMLongPressDragCellCollectionViewDelegate, BMLongPress

@property (weak, nonatomic) IBOutlet BMLongPressDragCellCollectionView *dragCellCollectionView;
@property (strong, nonatomic) UICollectionViewFlowLayout *collectionViewFlowLayout;
@property (strong, nonatomic) NSMutableArray <BMAlipayModel *> *dataSourceArray;
@property (strong, nonatomic) NSMutableArray <NSArray <BMAlipayModel *> *> *dataSourceArray;

@end

Expand Down Expand Up @@ -69,23 +69,22 @@ - (void)dragCellAlpha {
[self presentViewController:alertVC animated:YES completion:nil];
}

- (NSMutableArray<BMAlipayModel *> *)dataSourceArray {

- (NSMutableArray<NSArray<BMAlipayModel *> *> *)dataSourceArray {
if (!_dataSourceArray) {
_dataSourceArray = [@[] mutableCopy];

[_dataSourceArray addObject:[BMAlipayModel modelWithTitle:@"转账" iconName:@"转账"]];
[_dataSourceArray addObject:[BMAlipayModel modelWithTitle:@"信用卡还款" iconName:@"信用卡"]];
[_dataSourceArray addObject:[BMAlipayModel modelWithTitle:@"充值中心" iconName:@"充值中心"]];
[_dataSourceArray addObject:[BMAlipayModel modelWithTitle:@"芝麻信用" iconName:@"芝麻信用"]];

[_dataSourceArray addObject:[BMAlipayModel modelWithTitle:@"共享单车" iconName:@"091共享单车 copy"]];
[_dataSourceArray addObject:[BMAlipayModel modelWithTitle:@"花呗" iconName:@"花呗"]];
[_dataSourceArray addObject:[BMAlipayModel modelWithTitle:@"滴滴出行" iconName:@"滴滴出行"]];
[_dataSourceArray addObject:[BMAlipayModel modelWithTitle:@"火车票机票" iconName:@"火车票"]];

[_dataSourceArray addObject:[BMAlipayModel modelWithTitle:@"来分期" iconName:@"分期"]];
[_dataSourceArray addObject:[BMAlipayModel modelWithTitle:@"商家服务" iconName:@"商家服务2"]];
[_dataSourceArray addObject:[BMAlipayModel modelWithTitle:@"ofo小黄车" iconName:@"ofo共享单车"]];
NSMutableArray <BMAlipayModel *> *arr = [@[] mutableCopy];
[arr addObject:[BMAlipayModel modelWithTitle:@"转账" iconName:@"转账"]];
[arr addObject:[BMAlipayModel modelWithTitle:@"信用卡还款" iconName:@"信用卡"]];
[arr addObject:[BMAlipayModel modelWithTitle:@"充值中心" iconName:@"充值中心"]];
[arr addObject:[BMAlipayModel modelWithTitle:@"芝麻信用" iconName:@"芝麻信用"]];
[arr addObject:[BMAlipayModel modelWithTitle:@"共享单车" iconName:@"091共享单车 copy"]];
[arr addObject:[BMAlipayModel modelWithTitle:@"花呗" iconName:@"花呗"]];
[arr addObject:[BMAlipayModel modelWithTitle:@"滴滴出行" iconName:@"滴滴出行"]];
[arr addObject:[BMAlipayModel modelWithTitle:@"火车票机票" iconName:@"火车票"]];
[arr addObject:[BMAlipayModel modelWithTitle:@"来分期" iconName:@"分期"]];
[arr addObject:[BMAlipayModel modelWithTitle:@"商家服务" iconName:@"商家服务2"]];
[arr addObject:[BMAlipayModel modelWithTitle:@"ofo小黄车" iconName:@"ofo共享单车"]];
_dataSourceArray = @[arr].mutableCopy;
}
return _dataSourceArray;
}
Expand All @@ -104,25 +103,31 @@ - (UICollectionViewFlowLayout *)collectionViewFlowLayout {
return _collectionViewFlowLayout;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return self.dataSourceArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dataSourceArray[section].count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
return [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
}


- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(BMAlipayCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
cell.model = self.dataSourceArray[indexPath.row];
cell.model = self.dataSourceArray[indexPath.section][indexPath.item];
}

// === BMLongPressDragCellCollectionView 的核心方法 ↓

- (NSArray *)dataSourceWithDragCellCollectionView:(BMLongPressDragCellCollectionView *)dragCellCollectionView {
- (NSArray<NSArray<id> *> *)dataSourceWithDragCellCollectionView:(__kindof BMLongPressDragCellCollectionView *)dragCellCollectionView {
return self.dataSourceArray;
}

- (void)dragCellCollectionView:(BMLongPressDragCellCollectionView *)dragCellCollectionView newDataArrayAfterMove:(nullable NSArray *)newDataArray {

- (void)dragCellCollectionView:(__kindof BMLongPressDragCellCollectionView *)dragCellCollectionView newDataArrayAfterMove:(NSArray<NSArray<id> *> *)newDataArray {
self.dataSourceArray = [newDataArray mutableCopy];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(BMAli
cell.model = self.dataSourceArray[indexPath.section][indexPath.item];
}

- (NSArray *)dataSourceWithDragCellCollectionView:(BMLongPressDragCellCollectionView *)dragCellCollectionView {
- (NSArray<NSArray<id> *> *)dataSourceWithDragCellCollectionView:(__kindof BMLongPressDragCellCollectionView *)dragCellCollectionView {
return self.dataSourceArray;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ @interface BMQQImageVC () <BMLongPressDragCellCollectionViewDelegate, BMLongPres

@property (weak, nonatomic) IBOutlet BMLongPressDragCellCollectionView *imageCollectionView;
@property (strong, nonatomic) UICollectionViewFlowLayout *collectionViewFlowLayout;
@property (strong, nonatomic) NSMutableArray <NSDictionary *>*dataSourceArray;
@property (strong, nonatomic) NSMutableArray <NSArray <NSDictionary *> *> *dataSourceArray;

@end

Expand Down Expand Up @@ -44,41 +44,46 @@ - (void)viewDidLoad {
[self.imageCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass(BMImageCell.class) bundle:nil] forCellWithReuseIdentifier:imageIdentifier];
}

- (NSMutableArray<NSDictionary *> *)dataSourceArray {
- (NSMutableArray<NSArray<NSDictionary *> *> *)dataSourceArray {
if (!_dataSourceArray) {
_dataSourceArray = [@[] mutableCopy];
NSMutableArray <NSDictionary *> *arr = [@[] mutableCopy];
int sec = 193;
while (--sec > 0) {
CGFloat width = arc4random_uniform(60) + 40;
[_dataSourceArray addObject:@{@"icon" : [NSString stringWithFormat:@"%03d.png", sec], @"size" : [NSValue valueWithCGSize:CGSizeMake(width, width)]}];
[arr addObject:@{@"icon" : [NSString stringWithFormat:@"%03d.png", sec], @"size" : [NSValue valueWithCGSize:CGSizeMake(width, width)]}];
}
_dataSourceArray = @[arr].mutableCopy;
}
return _dataSourceArray;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return self.dataSourceArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.dataSourceArray count];
return self.dataSourceArray[section].count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
BMImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:imageIdentifier forIndexPath:indexPath];
UIImage *image = [UIImage imageNamed:self.dataSourceArray[indexPath.row][@"icon"]];
UIImage *image = [UIImage imageNamed:self.dataSourceArray[indexPath.section][indexPath.item][@"icon"]];
if (!image) {
image = [UIImage imageNamed:@"001.png"];
}
cell.imageView.image = image;
return cell;
}

- (NSArray *)dataSourceWithDragCellCollectionView:(BMLongPressDragCellCollectionView *)dragCellCollectionView {
- (NSArray<NSArray<id> *> *)dataSourceWithDragCellCollectionView:(__kindof BMLongPressDragCellCollectionView *)dragCellCollectionView {
return self.dataSourceArray;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return [self.dataSourceArray[indexPath.row][@"size"] CGSizeValue];
return [self.dataSourceArray[indexPath.section][indexPath.item][@"size"] CGSizeValue];
}

- (void)dragCellCollectionView:(BMLongPressDragCellCollectionView *)dragCellCollectionView newDataArrayAfterMove:(NSArray *)newDataArray {
- (void)dragCellCollectionView:(__kindof BMLongPressDragCellCollectionView *)dragCellCollectionView newDataArrayAfterMove:(NSArray<NSArray<id> *> *)newDataArray {
self.dataSourceArray = [newDataArray mutableCopy];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
return cell;
}

- (NSArray *)dataSourceWithDragCellCollectionView:(BMLongPressDragCellCollectionView *)dragCellCollectionView {
- (NSArray<NSArray<id> *> *)dataSourceWithDragCellCollectionView:(__kindof BMLongPressDragCellCollectionView *)dragCellCollectionView {
return self.dataSource;
}

- (void)dragCellCollectionView:(BMLongPressDragCellCollectionView *)dragCellCollectionView newDataArrayAfterMove:(NSArray *)newDataArray {

- (void)dragCellCollectionView:(__kindof BMLongPressDragCellCollectionView *)dragCellCollectionView newDataArrayAfterMove:(NSArray<NSArray<id> *> *)newDataArray {
self.dataSource = [newDataArray mutableCopy];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPa

#pragma mark - BMLongPressDragCellCollectionViewDataSource

- (NSArray *)dataSourceWithDragCellCollectionView:(BMLongPressDragCellCollectionView *)dragCellCollectionView {
- (NSArray<NSArray<id> *> *)dataSourceWithDragCellCollectionView:(__kindof BMLongPressDragCellCollectionView *)dragCellCollectionView {
return self.channelModelArray;
}

Expand Down Expand Up @@ -214,7 +214,7 @@ - (BOOL)dragCellCollectionViewShouldBeginExchange:(BMLongPressDragCellCollection
}

/// Cell 有交换时调用
- (void)dragCellCollectionView:(BMLongPressDragCellCollectionView *)dragCellCollectionView newDataArrayAfterMove:(NSArray *)newDataArray {
- (void)dragCellCollectionView:(__kindof BMLongPressDragCellCollectionView *)dragCellCollectionView newDataArrayAfterMove:(NSArray<NSArray<id> *> *)newDataArray {
self.channelModelArray = [newDataArray mutableCopy];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ class SwiftDemoCollectionViewCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: .zero)
contentView.addSubview(descLabel)
descLabel.backgroundColor = .darkGray
descLabel.layer.cornerRadius = 20
descLabel.layer.masksToBounds = true
descLabel.backgroundColor = .init(red: 0, green: 0, blue: 0, alpha: 0.2)
descLabel.textAlignment = .center
descLabel.snp.makeConstraints { (make) in
make.edges.equalTo(10)
make.edges.equalToSuperview().inset(10)
}
descLabel.layer.cornerRadius = 15
descLabel.layer.masksToBounds = true
}

required init?(coder: NSCoder) {
Expand Down
Loading

0 comments on commit 8a8bb6f

Please sign in to comment.