Skip to content

Commit

Permalink
Fix a view reusing issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenofex committed May 9, 2012
1 parent 5719a0c commit fd2f8ea
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
14 changes: 8 additions & 6 deletions StreamView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand All @@ -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 */
Expand All @@ -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 = "<group>"; };
4527EDB5155A4AA10000F8CC /* EKStreamView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EKStreamView.m; sourceTree = "<group>"; };
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; };
Expand All @@ -58,8 +60,6 @@
4595430C14C55661002BFF69 /* EKViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = EKViewController.xib; sourceTree = "<group>"; };
4595430D14C55661002BFF69 /* MyCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MyCell.h; sourceTree = "<group>"; };
4595430E14C55661002BFF69 /* MyCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MyCell.m; sourceTree = "<group>"; };
4595430F14C55661002BFF69 /* EKStreamView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EKStreamView.h; sourceTree = "<group>"; };
4595431014C55661002BFF69 /* EKStreamView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EKStreamView.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
};
Expand Down Expand Up @@ -438,6 +438,7 @@
4595430014C55616002BFF69 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
4595430114C55616002BFF69 /* Build configuration list for PBXNativeTarget "StreamViewTests" */ = {
isa = XCConfigurationList;
Expand All @@ -446,6 +447,7 @@
4595430314C55616002BFF69 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
3 changes: 3 additions & 0 deletions StreamView/EKStreamView.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,15 @@
NSMutableDictionary *cellCache; // reuseIdentifier => NSMutableArray
NSSet *visibleCellInfo;
UIView *headerView, *footerView;
CGFloat columnWidth;
}

@property (nonatomic, assign) id<EKStreamViewDelegate> delegate;
@property (nonatomic, assign) CGFloat columnPadding;
@property (nonatomic, assign) CGFloat cellPadding;

@property (nonatomic, readonly) CGFloat columnWidth;

- (id<EKResusableCell>)dequeueReusableCellWithIdentifier:(NSString *)identifier;
- (void)reloadData;

Expand Down
21 changes: 16 additions & 5 deletions StreamView/EKStreamView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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!"];

Expand All @@ -133,15 +139,14 @@ - (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]];
[rectsForCells addObject:[NSMutableArray arrayWithCapacity:20]];
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]];
Expand Down Expand Up @@ -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
Expand All @@ -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];
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fd2f8ea

Please sign in to comment.