From fd2f8ea6e6570717ea060ea3b2f59ef0a383759f Mon Sep 17 00:00:00 2001 From: Eli Wang Date: Wed, 9 May 2012 14:50:07 +0800 Subject: [PATCH] Fix a view reusing issue. --- StreamView.xcodeproj/project.pbxproj | 14 ++++++++------ StreamView/EKStreamView.h | 3 +++ StreamView/EKStreamView.m | 21 ++++++++++++++++----- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/StreamView.xcodeproj/project.pbxproj b/StreamView.xcodeproj/project.pbxproj index 6e2e717..7f89606 100644 --- a/StreamView.xcodeproj/project.pbxproj +++ b/StreamView.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 4527EDB6155A4AA10000F8CC /* EKStreamView.m in Sources */ = {isa = PBXBuildFile; fileRef = 4527EDB5155A4AA10000F8CC /* EKStreamView.m */; }; 459542D014C55616002BFF69 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 459542CF14C55616002BFF69 /* UIKit.framework */; }; 459542D214C55616002BFF69 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 459542D114C55616002BFF69 /* Foundation.framework */; }; 459542D414C55616002BFF69 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 459542D314C55616002BFF69 /* CoreGraphics.framework */; }; @@ -22,7 +23,6 @@ 4595431114C55661002BFF69 /* EKViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4595430B14C55661002BFF69 /* EKViewController.m */; }; 4595431214C55661002BFF69 /* EKViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4595430C14C55661002BFF69 /* EKViewController.xib */; }; 4595431314C55661002BFF69 /* MyCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 4595430E14C55661002BFF69 /* MyCell.m */; }; - 4595431414C55661002BFF69 /* EKStreamView.m in Sources */ = {isa = PBXBuildFile; fileRef = 4595431014C55661002BFF69 /* EKStreamView.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -36,6 +36,8 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 4527EDB4155A4AA10000F8CC /* EKStreamView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EKStreamView.h; sourceTree = ""; }; + 4527EDB5155A4AA10000F8CC /* EKStreamView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EKStreamView.m; sourceTree = ""; }; 459542CB14C55616002BFF69 /* StreamView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = StreamView.app; sourceTree = BUILT_PRODUCTS_DIR; }; 459542CF14C55616002BFF69 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; }; 459542D114C55616002BFF69 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; @@ -58,8 +60,6 @@ 4595430C14C55661002BFF69 /* EKViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = EKViewController.xib; sourceTree = ""; }; 4595430D14C55661002BFF69 /* MyCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MyCell.h; sourceTree = ""; }; 4595430E14C55661002BFF69 /* MyCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MyCell.m; sourceTree = ""; }; - 4595430F14C55661002BFF69 /* EKStreamView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EKStreamView.h; sourceTree = ""; }; - 4595431014C55661002BFF69 /* EKStreamView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EKStreamView.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -128,8 +128,8 @@ 4595430C14C55661002BFF69 /* EKViewController.xib */, 4595430D14C55661002BFF69 /* MyCell.h */, 4595430E14C55661002BFF69 /* MyCell.m */, - 4595430F14C55661002BFF69 /* EKStreamView.h */, - 4595431014C55661002BFF69 /* EKStreamView.m */, + 4527EDB4155A4AA10000F8CC /* EKStreamView.h */, + 4527EDB5155A4AA10000F8CC /* EKStreamView.m */, 459542D614C55616002BFF69 /* Supporting Files */, ); path = StreamView; @@ -276,7 +276,7 @@ 459542E014C55616002BFF69 /* EKAppDelegate.m in Sources */, 4595431114C55661002BFF69 /* EKViewController.m in Sources */, 4595431314C55661002BFF69 /* MyCell.m in Sources */, - 4595431414C55661002BFF69 /* EKStreamView.m in Sources */, + 4527EDB6155A4AA10000F8CC /* EKStreamView.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -438,6 +438,7 @@ 4595430014C55616002BFF69 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; 4595430114C55616002BFF69 /* Build configuration list for PBXNativeTarget "StreamViewTests" */ = { isa = XCConfigurationList; @@ -446,6 +447,7 @@ 4595430314C55616002BFF69 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/StreamView/EKStreamView.h b/StreamView/EKStreamView.h index 9941e49..795c4e9 100644 --- a/StreamView/EKStreamView.h +++ b/StreamView/EKStreamView.h @@ -64,12 +64,15 @@ NSMutableDictionary *cellCache; // reuseIdentifier => NSMutableArray NSSet *visibleCellInfo; UIView *headerView, *footerView; + CGFloat columnWidth; } @property (nonatomic, assign) id delegate; @property (nonatomic, assign) CGFloat columnPadding; @property (nonatomic, assign) CGFloat cellPadding; +@property (nonatomic, readonly) CGFloat columnWidth; + - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; - (void)reloadData; diff --git a/StreamView/EKStreamView.m b/StreamView/EKStreamView.m index 4c5fc6b..2a52199 100644 --- a/StreamView/EKStreamView.m +++ b/StreamView/EKStreamView.m @@ -102,12 +102,16 @@ - (void)reloadData [headerView removeFromSuperview]; [footerView removeFromSuperview]; + for (EKStreamViewCellInfo *cellInfo in visibleCellInfo) { + [cellInfo.cell removeFromSuperview]; + } + if ([delegate respondsToSelector:@selector(headerForStreamView:)]) { headerView = [[delegate headerForStreamView:self] retain]; CGRect f = headerView.frame; f.origin = CGPointMake(columnPadding, cellPadding); headerView.frame = f; - + [self addSubview:headerView]; } else { headerView = nil; @@ -122,6 +126,8 @@ - (void)reloadData // calculate height for all cells NSInteger numberOfColumns = [delegate numberOfColumnsInStreamView:self]; + columnWidth = (self.frame.size.width - (numberOfColumns + 1) * self.columnPadding) / numberOfColumns; + if (numberOfColumns < 1) [NSException raise:NSInvalidArgumentException format:@"The number of columns must be equal or greater than 1!"]; @@ -133,7 +139,6 @@ - (void)reloadData } - CGFloat columnWidth = (self.frame.size.width - (columnPadding * (numberOfColumns + 1))) / numberOfColumns ; CGFloat cellHeight = headerView ? headerView.frame.size.height + cellPadding : cellPadding; for (int i = 0; i < numberOfColumns; i++) { [cellHeightsByColumn addObject:[NSMutableArray arrayWithCapacity:20]]; @@ -141,7 +146,7 @@ - (void)reloadData cellX[i] = (i == 0 ? columnPadding : cellX[i - 1] + columnWidth + columnPadding); columnHeights[i] = cellHeight; } - + for (int i = 0; i < numberOfCells; i++) { CGFloat height = [delegate streamView:self heightForCellAtIndex:i]; [cellHeightsByIndex addObject:[NSNumber numberWithFloat:height]]; @@ -210,6 +215,12 @@ - (void)reloadData return cell; } +- (CGFloat)columnWidth +{ + NSInteger numColumns = [delegate numberOfColumnsInStreamView:self]; + return (self.frame.size.width - (numColumns + 1) * self.columnPadding) / numColumns; +} + #pragma mark - Private Methods - (NSSet *)getVisibleCellInfo @@ -218,7 +229,7 @@ - (NSSet *)getVisibleCellInfo CGFloat offsetBottom = offsetTop + self.frame.size.height; NSMutableSet *ret = [NSMutableSet setWithCapacity:10]; - + for (NSMutableArray *rectsForCellsInCol in rectsForCells) { for (int i = 0, c = [rectsForCellsInCol count]; i < c; i++) { EKStreamViewCellInfo *info = [rectsForCellsInCol objectAtIndex:i]; @@ -256,7 +267,7 @@ - (void)setup cellHeightsByIndex = [[NSMutableArray alloc] initWithCapacity:30]; cellHeightsByColumn = [[NSMutableArray alloc] initWithCapacity:5]; rectsForCells = [[NSMutableArray alloc] initWithCapacity:5]; - cellCache = [[NSMutableDictionary alloc] initWithCapacity:5]; + cellCache = [[NSMutableDictionary alloc] initWithCapacity:20]; } @end