Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复自定义cell中的index不准确的bug #665

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>SDCycleScrollView.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
</dict>
</dict>
</dict>
</plist>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
uuid = "01CB7470-D3DE-40A8-A639-111186E08BA1"
type = "0"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.SymbolicBreakpoint">
<BreakpointContent
uuid = "485920DE-A6DC-4F99-AE49-B1691B7434A8"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = ""
moduleName = "">
<Locations>
</Locations>
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
91 changes: 50 additions & 41 deletions SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@
NSString * const ID = @"SDCycleScrollViewCell";

@interface SDCycleScrollView () <UICollectionViewDataSource, UICollectionViewDelegate>


{
struct DelegateHas{//由于考虑到自动轮播,会经常性使用respondsToSelector,因此使用结构体进行寄存respondsToSelector的结果
unsigned int didSelectItemAtIndex : 1;
unsigned int didScrollToIndex : 1;
unsigned int willDisplayCell : 1;
unsigned int customizeCell : 1;
};
}
@property (nonatomic, weak) UICollectionView *mainView; // 显示图片的collectionView
@property (nonatomic, weak) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSArray *imagePathsGroup;
Expand All @@ -52,6 +58,8 @@ @interface SDCycleScrollView () <UICollectionViewDataSource, UICollectionViewDel

@property (nonatomic, strong) UIImageView *backgroundImageView; // 当imageURLs为空时的背景图

@property (nonatomic, assign) struct DelegateHas delegateHas;

@end

@implementation SDCycleScrollView
Expand Down Expand Up @@ -156,12 +164,18 @@ - (void)setupMainView
- (void)setDelegate:(id<SDCycleScrollViewDelegate>)delegate
{
_delegate = delegate;

if ([self.delegate respondsToSelector:@selector(customCollectionViewCellClassForCycleScrollView:)] && [self.delegate customCollectionViewCellClassForCycleScrollView:self]) {
[self.mainView registerClass:[self.delegate customCollectionViewCellClassForCycleScrollView:self] forCellWithReuseIdentifier:ID];
}else if ([self.delegate respondsToSelector:@selector(customCollectionViewCellNibForCycleScrollView:)] && [self.delegate customCollectionViewCellNibForCycleScrollView:self]) {
[self.mainView registerNib:[self.delegate customCollectionViewCellNibForCycleScrollView:self] forCellWithReuseIdentifier:ID];

_delegateHas.customizeCell = 0;
if (delegate && [delegate respondsToSelector:@selector(customCollectionViewCellClassForCycleScrollView:)] && [delegate customCollectionViewCellClassForCycleScrollView:self]) {
_delegateHas.customizeCell = 1;
[self.mainView registerClass:[delegate customCollectionViewCellClassForCycleScrollView:self] forCellWithReuseIdentifier:ID];
}else if(delegate && [delegate respondsToSelector:@selector(customCollectionViewCellNibForCycleScrollView:)] && [delegate customCollectionViewCellNibForCycleScrollView:self]){
_delegateHas.customizeCell = 1;
[self.mainView registerNib:[delegate customCollectionViewCellNibForCycleScrollView:self] forCellWithReuseIdentifier:ID];
}
_delegateHas.didSelectItemAtIndex = delegate && [delegate respondsToSelector:@selector(cycleScrollView:didSelectItemAtIndex:)];
_delegateHas.didScrollToIndex = delegate && [delegate respondsToSelector:@selector(cycleScrollView:didScrollToIndex:)];
_delegateHas.willDisplayCell = delegate && [delegate respondsToSelector:@selector(setupCustomCell:forIndex:cycleScrollView:)];
}

- (void)setPlaceholderImage:(UIImage *)placeholderImage
Expand Down Expand Up @@ -568,58 +582,52 @@ - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSe

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
SDCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];//SDCollectionViewCell
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
long itemIndex = [self pageControlIndexWithCurrentCellIndex:indexPath.item];

if ([self.delegate respondsToSelector:@selector(setupCustomCell:forIndex:cycleScrollView:)] &&
[self.delegate respondsToSelector:@selector(customCollectionViewCellClassForCycleScrollView:)] && [self.delegate customCollectionViewCellClassForCycleScrollView:self]) {
[self.delegate setupCustomCell:cell forIndex:itemIndex cycleScrollView:self];
return cell;
}else if ([self.delegate respondsToSelector:@selector(setupCustomCell:forIndex:cycleScrollView:)] &&
[self.delegate respondsToSelector:@selector(customCollectionViewCellNibForCycleScrollView:)] && [self.delegate customCollectionViewCellNibForCycleScrollView:self]) {
[self.delegate setupCustomCell:cell forIndex:itemIndex cycleScrollView:self];
return cell;
if (_delegateHas.customizeCell) {//自定义cell
if (_delegateHas.willDisplayCell) {
[_delegate setupCustomCell:cell forIndex:itemIndex cycleScrollView:self];
}
return;
}

NSString *imagePath = self.imagePathsGroup[itemIndex];

SDCollectionViewCell *systemCell = (SDCollectionViewCell *)cell;
if (!self.onlyDisplayText && [imagePath isKindOfClass:[NSString class]]) {
if ([imagePath hasPrefix:@"http"]) {
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:imagePath] placeholderImage:self.placeholderImage];
[systemCell.imageView sd_setImageWithURL:[NSURL URLWithString:imagePath] placeholderImage:self.placeholderImage];
} else {
UIImage *image = [UIImage imageNamed:imagePath];
if (!image) {
image = [UIImage imageWithContentsOfFile:imagePath];
}
cell.imageView.image = image;
systemCell.imageView.image = image;
}
} else if (!self.onlyDisplayText && [imagePath isKindOfClass:[UIImage class]]) {
cell.imageView.image = (UIImage *)imagePath;
systemCell.imageView.image = (UIImage *)imagePath;
}

if (_titlesGroup.count && itemIndex < _titlesGroup.count) {
cell.title = _titlesGroup[itemIndex];
systemCell.title = _titlesGroup[itemIndex];
}

if (!cell.hasConfigured) {
cell.titleLabelBackgroundColor = self.titleLabelBackgroundColor;
cell.titleLabelHeight = self.titleLabelHeight;
cell.titleLabelTextAlignment = self.titleLabelTextAlignment;
cell.titleLabelTextColor = self.titleLabelTextColor;
cell.titleLabelTextFont = self.titleLabelTextFont;
cell.hasConfigured = YES;
cell.imageView.contentMode = self.bannerImageViewContentMode;
cell.clipsToBounds = YES;
cell.onlyDisplayText = self.onlyDisplayText;
if (!systemCell.hasConfigured) {
systemCell.titleLabelBackgroundColor = self.titleLabelBackgroundColor;
systemCell.titleLabelHeight = self.titleLabelHeight;
systemCell.titleLabelTextAlignment = self.titleLabelTextAlignment;
systemCell.titleLabelTextColor = self.titleLabelTextColor;
systemCell.titleLabelTextFont = self.titleLabelTextFont;
systemCell.hasConfigured = YES;
systemCell.imageView.contentMode = self.bannerImageViewContentMode;
systemCell.clipsToBounds = YES;
systemCell.onlyDisplayText = self.onlyDisplayText;
}

return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.delegate respondsToSelector:@selector(cycleScrollView:didSelectItemAtIndex:)]) {
if (_delegateHas.didSelectItemAtIndex) {
[self.delegate cycleScrollView:self didSelectItemAtIndex:[self pageControlIndexWithCurrentCellIndex:indexPath.item]];
}
if (self.clickItemOperationBlock) {
Expand Down Expand Up @@ -670,10 +678,11 @@ - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
int itemIndex = [self currentIndex];
int indexOnPageControl = [self pageControlIndexWithCurrentCellIndex:itemIndex];

if ([self.delegate respondsToSelector:@selector(cycleScrollView:didScrollToIndex:)]) {
if (_delegateHas.didScrollToIndex) {
[self.delegate cycleScrollView:self didScrollToIndex:indexOnPageControl];
} else if (self.itemDidScrollOperationBlock) {
self.itemDidScrollOperationBlock(indexOnPageControl);
}
if (self.itemDidScrollOperationBlock) {
self.itemDidScrollOperationBlock(indexOnPageControl);
}
}

Expand Down