Skip to content

Commit

Permalink
更新返回的 IndexPath 替换成局部的索引
Browse files Browse the repository at this point in the history
  • Loading branch information
josercc committed Jan 26, 2018
1 parent da14251 commit 7498d57
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 17 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file removed ZHTableViewGroup/.DS_Store
Binary file not shown.
Empty file removed ZHTableViewGroup/Assets/.gitkeep
Empty file.
Empty file removed ZHTableViewGroup/Classes/.gitkeep
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

typedef NS_ENUM(NSUInteger, ZHTableViewCustomHeightType) {
ZHTableViewCustomHeightTypeCell,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ - (ZHTableViewDataSourceCustomHeightCompletionHandle)completionHandleWithTableVi
if (!model.customHeightCompletionHandle) {
return model.height;
}
return model.customHeightCompletionHandle(tableView,indexPath,model);
return model.customHeightCompletionHandle(tableView,[ZHTableViewDataSource indexPathWithDataSource:_dataSource indexPath:indexPath],model);
};
return completionHandle;
}
Expand Down
1 change: 0 additions & 1 deletion ZHTableViewGroup/Classes/ZHTableViewBaseModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// Created by 张行 on 2017/3/18.
//
//
#import <UIKit/UIKit.h>

@class ZHTableViewBaseModel;

Expand Down
2 changes: 1 addition & 1 deletion ZHTableViewGroup/Classes/ZHTableViewCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by 张行 on 2017/3/18.
//
//
#import <UIKit/UIKit.h>

#import "ZHTableViewBaseModel.h"

/**
Expand Down
4 changes: 3 additions & 1 deletion ZHTableViewGroup/Classes/ZHTableViewDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "ZHTableViewGroup.h"
#import "ZHTableViewGroup.h"
#import "ZHTableViewCell.h"
Expand Down Expand Up @@ -144,4 +144,6 @@ typedef CGFloat (^ZHTableViewDataSourceCustomHeightCompletionHandle)(ZHTableView
*/
- (void)clearData;

+ (NSIndexPath *)indexPathWithDataSource:(ZHTableViewDataSource *)dataSource indexPath:(NSIndexPath *)indexPath;

@end
34 changes: 26 additions & 8 deletions ZHTableViewGroup/Classes/ZHTableViewDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ + (CGFloat)heightForRowAtDataSource:(ZHTableViewDataSource *)dataSource
if (!cell) {
return 0;
}
UITableViewCell *automaticHeightCell = [self cellForRowAtWithDataSource:dataSource indexPath:indexPath];
CGFloat automaticHeight = [automaticHeightCell sizeThatFits:CGSizeMake([UIScreen mainScreen].bounds.size.width, CGFLOAT_MAX)].height;
if (cell.height == NSNotFound && automaticHeight != CGFLOAT_MAX) {
cell.height = automaticHeight;
}
return [self heightWithCustomHandle:cell.height customCompletionHandle:customHeightCompletionHandle baseModel:cell];
}

Expand All @@ -94,7 +99,8 @@ + (void)didSelectRowAtWithDataSource:(ZHTableViewDataSource *)dataSource
return;
}
UITableViewCell *cell = [self cellForRowAtWithDataSource:dataSource indexPath:indexPath];
[tableViewCell didSelectRowAtWithCell:cell indexPath:indexPath];
ZHTableViewGroup *group = [self groupForSectionWithDataSource:dataSource section:indexPath.section];
[tableViewCell didSelectRowAtWithCell:cell indexPath:[group indexPathWithCell:tableViewCell indexPath:indexPath]];
}

+ (CGFloat)heightForHeaderInSectionWithDataSource:(ZHTableViewDataSource *)dataSource
Expand Down Expand Up @@ -139,10 +145,13 @@ + (CGFloat)heightForHeaderFooterInSectionWithDataSource:(ZHTableViewDataSource *
}
NSInteger height = 0;
ZHTableViewBaseModel *baseModel;
UITableViewHeaderFooterView *headFooter = [self viewHeaderFooterInSectionWithDtaSource:dataSource section:section style:style];
CGFloat automaticHeight = [headFooter sizeThatFits:CGSizeMake([UIScreen mainScreen].bounds.size.width, CGFLOAT_MAX)].height;
switch (style) {
case ZHTableViewHeaderFooterStyleHeader: {
height = group.header.height;
baseModel = group.header;

}
break;
case ZHTableViewHeaderFooterStyleFooter: {
Expand All @@ -151,19 +160,22 @@ + (CGFloat)heightForHeaderFooterInSectionWithDataSource:(ZHTableViewDataSource *
}
break;
}
if (height == NSNotFound && automaticHeight != CGFLOAT_MAX) {
height = automaticHeight;
}
return [self heightWithCustomHandle:height customCompletionHandle:customHeightCompletionHandle baseModel:baseModel];
}

+ (CGFloat)heightWithCustomHandle:(CGFloat)height
customCompletionHandle:(ZHTableViewDataSourceCustomHeightCompletionHandle)customCompletionHandle
baseModel:(ZHTableViewBaseModel *)baseModel {
if (height == NSNotFound) {
if (customCompletionHandle) {
return customCompletionHandle(baseModel);
}
return 0;
}
return height;
if (customCompletionHandle) {
return customCompletionHandle(baseModel);
}
if (height != 0) {
return height;
}
return 44;
}


Expand All @@ -187,6 +199,12 @@ + (ZHTableViewCell *)cellForIndexPath:(ZHTableViewDataSource *)dataSource
return [group tableViewCellForIndexPath:indexPath];
}

+ (NSIndexPath *)indexPathWithDataSource:(ZHTableViewDataSource *)dataSource indexPath:(NSIndexPath *)indexPath {
ZHTableViewGroup *group = [self groupForSectionWithDataSource:dataSource section:indexPath.section];
ZHTableViewCell *tableViewCell = [self cellForIndexPath:dataSource indexPath:indexPath];
return [group indexPathWithCell:tableViewCell indexPath:indexPath];
}

- (void)registerClasss {
for (ZHTableViewGroup *group in self.groups) {
[group registerHeaderFooterCellWithTableView:self.tableView];
Expand Down
3 changes: 2 additions & 1 deletion ZHTableViewGroup/Classes/ZHTableViewGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "ZHTableViewHeaderFooter.h"
#import "ZHTableViewCell.h"

Expand All @@ -33,4 +33,5 @@ typedef void(^ZHTableViewGroupAddHeaderFooterCompletionHandle)(ZHTableViewHeader

- (UITableViewHeaderFooterView *)headerFooterForStyle:(ZHTableViewHeaderFooterStyle)style tableView:(UITableView *)tableView section:(NSUInteger)section;

- (NSIndexPath *)indexPathWithCell:(ZHTableViewCell *)cell indexPath:(NSIndexPath *)indexPath;
@end
14 changes: 13 additions & 1 deletion ZHTableViewGroup/Classes/ZHTableViewGroup.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,22 @@ - (UITableViewCell *)cellForTableViewWithTableView:(UITableView *)tableView inde
return nil;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableViewCell.identifier];
[tableViewCell configCellWithCell:cell indexPath:indexPath];
[tableViewCell configCellWithCell:cell indexPath:[self indexPathWithCell:tableViewCell indexPath:indexPath]];
return cell;
}

- (NSIndexPath *)indexPathWithCell:(ZHTableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
__block NSUInteger startIndex = 0;
[self.cells enumerateObjectsUsingBlock:^(ZHTableViewCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj == cell) {
*stop = YES;
return;
}
startIndex += obj.cellNumber;
}];
return [NSIndexPath indexPathForRow:indexPath.row - startIndex inSection:indexPath.section];
}

- (ZHTableViewCell *)tableViewCellForIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row >= self.cellCount) {
return nil;
Expand Down
2 changes: 1 addition & 1 deletion ZHTableViewGroup/Classes/ZHTableViewHeaderFooter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by 张行 on 2017/3/18.
//
//
#import <UIKit/UIKit.h>

#import "ZHTableViewBaseModel.h"

/**
Expand Down
2 changes: 1 addition & 1 deletion ZHTableViewGroupObjc.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'ZHTableViewGroupObjc'
s.version = '2.6.0'
s.version = '2.7.0'
s.summary = 'Manger UITableView DataSource More Cell Style'

s.homepage = 'https://github.com/josercc/ZHTableViewGroup'
Expand Down

0 comments on commit 7498d57

Please sign in to comment.