Skip to content

Commit 6dde12b

Browse files
committed
cell选择样式逻辑修改,默认选择样式无,选择模式下为默认模式(选择背景已去掉)。修复cellForRow中实现cellForRow代理后model为nil的bug。提升选择模式下默认代理样式优先级
1 parent d15738d commit 6dde12b

File tree

7 files changed

+52
-12
lines changed

7 files changed

+52
-12
lines changed

DEMO/Podfile.lock

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
PODS:
2+
- DWAnimation (1.1.10)
3+
- Masonry (1.0.2)
4+
5+
DEPENDENCIES:
6+
- DWAnimation (~> 1.1.10)
7+
- Masonry (~> 1.0.2)
8+
9+
SPEC CHECKSUMS:
10+
DWAnimation: 2f0a7456ec9fff2da2e4d1352552b6cd593d00a9
11+
Masonry: 7c429b56da9d4ee0bbb3ed77a5ea710d6a5df39e
12+
13+
PODFILE CHECKSUM: 30497533825386c1222b0a097d1e87a99eab6b69
14+
15+
COCOAPODS: 1.2.1

DEMO/a.xcodeproj/project.pbxproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
9B9DAC7B1EBEE62D00196BC7 /* BCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B9DAC7A1EBEE62D00196BC7 /* BCell.m */; };
2222
9B9DAC7F1EBEEE6B00196BC7 /* CCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 9B9DAC7D1EBEEE6A00196BC7 /* CCell.m */; };
2323
9B9DAC801EBEEE6B00196BC7 /* CCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9B9DAC7E1EBEEE6A00196BC7 /* CCell.xib */; };
24-
9BFEB7C61EC711DF00EDE288 /* icon.png in Resources */ = {isa = PBXBuildFile; fileRef = 9BFEB7C51EC711DF00EDE288 /* icon.png */; };
2524
9BFEB7D81EC72FC600EDE288 /* DWTableViewHelperResource.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 9BFEB7D71EC72FC600EDE288 /* DWTableViewHelperResource.bundle */; };
2625
/* End PBXBuildFile section */
2726

@@ -51,7 +50,6 @@
5150
9B9DAC7C1EBEEE6A00196BC7 /* CCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCell.h; sourceTree = "<group>"; };
5251
9B9DAC7D1EBEEE6A00196BC7 /* CCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCell.m; sourceTree = "<group>"; };
5352
9B9DAC7E1EBEEE6A00196BC7 /* CCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = CCell.xib; sourceTree = "<group>"; };
54-
9BFEB7C51EC711DF00EDE288 /* icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = icon.png; path = ../../../../../icon.png; sourceTree = "<group>"; };
5553
9BFEB7D71EC72FC600EDE288 /* DWTableViewHelperResource.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = DWTableViewHelperResource.bundle; sourceTree = "<group>"; };
5654
B205B3A7DA99B939C9B6EA02 /* Pods-a.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-a.debug.xcconfig"; path = "Pods/Target Support Files/Pods-a/Pods-a.debug.xcconfig"; sourceTree = "<group>"; };
5755
/* End PBXFileReference section */
@@ -113,7 +111,6 @@
113111
9B9DAC7C1EBEEE6A00196BC7 /* CCell.h */,
114112
9B9DAC7D1EBEEE6A00196BC7 /* CCell.m */,
115113
9B9DAC7E1EBEEE6A00196BC7 /* CCell.xib */,
116-
9BFEB7C51EC711DF00EDE288 /* icon.png */,
117114
9B4926C31EBD77C700EF58DE /* AppDelegate.h */,
118115
9B4926C41EBD77C700EF58DE /* AppDelegate.m */,
119116
9B4926C61EBD77C700EF58DE /* ViewController.h */,
@@ -205,7 +202,6 @@
205202
buildActionMask = 2147483647;
206203
files = (
207204
9B4926D01EBD77C700EF58DE /* LaunchScreen.storyboard in Resources */,
208-
9BFEB7C61EC711DF00EDE288 /* icon.png in Resources */,
209205
9B9DAC801EBEEE6B00196BC7 /* CCell.xib in Resources */,
210206
9B4926CD1EBD77C700EF58DE /* Assets.xcassets in Resources */,
211207
9B4926CB1EBD77C700EF58DE /* Main.storyboard in Resources */,

DEMO/a.xcworkspace/contents.xcworkspacedata

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Bucket
3+
type = "0"
4+
version = "2.0">
5+
</Bucket>

DEMO/a/ViewController.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ -(id)dw_TableView:(UITableView *)tableView showAnimationWithCell:(UITableViewCel
5959
}];
6060
}
6161

62+
//-(UITableViewCellEditingStyle)dw_TableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
63+
// return UITableViewCellEditingStyleInsert;
64+
//}
65+
6266
-(UITableView *)tabV {
6367
if (!_tabV) {
6468
_tabV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 414, 500) style:(UITableViewStyleGrouped)
@@ -68,7 +72,7 @@ -(UITableView *)tabV {
6872
[self.helper setTheSeperatorToZero];
6973
self.helper.minAutoRowHeight = 55;
7074
self.helper.helperDelegate = self;
71-
self.helper.loadDataMode = DWTableViewHelperLoadDataLazyMode;
75+
self.helper.loadDataMode = DWTableViewHelperLoadDataIgnoreHighSpeedMode;
7276
self.helper.ignoreCount = 1;
7377
}
7478
return _tabV;

DWTableViewHelper/DWTableViewHelper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170

171171
///cell占位图(仅优化模式下有效)
172172
-(UIImage *)dw_TableView:(UITableView *)tableView loadDataPlaceHolderForCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
173+
173174
@end
174175

175176
#pragma mark --- cell 基础属性协议---

DWTableViewHelper/DWTableViewHelper.m

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *
395395
{
396396
if (self.selectEnable) {
397397
if (!self.multiSelect && self.lastSelected) {
398-
[tableView deselectRowAtIndexPath:self.lastSelected animated:YES];
398+
[tableView deselectRowAtIndexPath:self.lastSelected animated:NO];
399399
}
400400
self.lastSelected = indexPath;
401401
return;
@@ -414,12 +414,12 @@ -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath
414414

415415
///editing
416416
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
417-
if (DWRespond) {
418-
return [DWDelegate dw_TableView:tableView editingStyleForRowAtIndexPath:indexPath];
419-
}
420417
if (self.selectEnable) {
421418
return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
422419
}
420+
if (DWRespond) {
421+
return [DWDelegate dw_TableView:tableView editingStyleForRowAtIndexPath:indexPath];
422+
}
423423
return UITableViewCellEditingStyleNone;
424424
}
425425

@@ -551,14 +551,13 @@ -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)
551551
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
552552
{
553553
DWTableViewHelperCell * cell = nil;
554-
DWTableViewHelperModel * model = nil;
554+
DWTableViewHelperModel * model = [self modelFromIndexPath:indexPath];
555555
if (DWRespond) {
556556
cell = [DWDelegate dw_TableView:tableView cellForRowAtIndexPath:indexPath];
557557
if (!cell) {
558558
NSAssert(NO, @"you have implemetation the dw_TableView:cellForRowAtIndexPath delegate but pass a nil cell at indexPath of S%ldR%ld",indexPath.section,indexPath.row);
559559
}
560560
} else {
561-
model = [self modelFromIndexPath:indexPath];
562561
cell = [self createCellFromModel:model useReuse:YES];
563562
}
564563
if (self.loadDataMode == DWTableViewHelperLoadDataIgnoreHighSpeedMode) {
@@ -1251,6 +1250,9 @@ @interface DWTableViewHelperModel ()
12511250
///计算的横屏行高
12521251
@property (nonatomic ,assign) CGFloat calRowHeightH;
12531252

1253+
///原始cell选择样式
1254+
@property (nonatomic ,assign) NSInteger originalSelectionStyle;
1255+
12541256
@end
12551257

12561258
@implementation DWTableViewHelperModel
@@ -1264,6 +1266,7 @@ -(instancetype)init{
12641266
if (!ImageNull) {
12651267
ImageNull = [UIImage new];
12661268
}
1269+
self.originalSelectionStyle = -1;
12671270
NSString * cellClass = NSStringFromClass([self class]);
12681271
NSArray * arr = [cellClass componentsSeparatedByString:@"Model"];
12691272
if (arr.count) {
@@ -1369,8 +1372,10 @@ -(void)layoutSubviews
13691372
///适配第一次图片为空的情况
13701373
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
13711374
[super setEditing:editing animated:animated];
1372-
if (self.selectionStyle == UITableViewCellSelectionStyleNone) {
1375+
if (editing && self.selectionStyle != UITableViewCellSelectionStyleDefault) {//编辑状态下保证非无选择样式
13731376
self.selectionStyle = UITableViewCellSelectionStyleDefault;
1377+
} else if (!editing && self.selectionStyle != self.model.originalSelectionStyle) {//退出编辑状态是恢复原始选择样式
1378+
self.selectionStyle = self.model.originalSelectionStyle;
13741379
}
13751380
BOOL toSetUnselectIcon = self.model.cellEditUnselectedIcon != ImageNull && self.model.cellEditUnselectedIcon != nil;
13761381
for (UIControl *control in self.subviews){
@@ -1400,6 +1405,7 @@ -(void)setupUI {
14001405
self.backgroundColor = [UIColor whiteColor];
14011406
self.multipleSelectionBackgroundView = [UIView new];
14021407
self.selectedBackgroundView = [UIView new];
1408+
self.selectionStyle = UITableViewCellSelectionStyleNone;
14031409
self.loadDataImageView = [UIImageView new];
14041410
self.loadDataImageView.backgroundColor = [UIColor whiteColor];
14051411
}
@@ -1411,6 +1417,9 @@ -(void)setupConstraints {
14111417
-(void)setModel:(__kindof DWTableViewHelperModel *)model
14121418
{
14131419
_model = model;
1420+
if (model.originalSelectionStyle == -1) {//仅在初次生成cell的时候同步cell的选择样式
1421+
model.originalSelectionStyle = self.selectionStyle;
1422+
}
14141423
}
14151424

14161425
-(void)showLoadDataPlaceHolder:(UIImage *)image height:(CGFloat)height{

0 commit comments

Comments
 (0)