Skip to content
Browse files

Added highlight flag to drawContentView to allow views to nicely fade…

… in and out during the selected animation.
  • Loading branch information...
1 parent 4e98616 commit 87128d7f8859ec4d83fb62229ae3dbd83fb19d4f @shnhrrsn shnhrrsn committed
Showing with 107 additions and 21 deletions.
  1. +4 −4 ABTableViewCell.h
  2. +103 −17 ABTableViewCell.m
View
8 ABTableViewCell.h
@@ -31,11 +31,11 @@
// to use: subclass ABTableViewCell and implement -drawContentView:
-@interface ABTableViewCell : UITableViewCell
-{
- UIView *contentView;
+@interface ABTableViewCell : UITableViewCell {
+ UIView* contentView;
+ UIView* selectedContentView;
}
-- (void)drawContentView:(CGRect)r; // subclasses should implement
+- (void)drawContentView:(CGRect)rect highlighted:(BOOL)highlighted; // subclasses should implement
@end
View
120 ABTableViewCell.m
@@ -32,52 +32,138 @@
@interface ABTableViewCellView : UIView
@end
+@interface ABTableViewSelectedCellView : UIView
+@end
+
@implementation ABTableViewCellView
-- (void)drawRect:(CGRect)r
-{
- [(ABTableViewCell *)[self superview] drawContentView:r];
+- (id)initWithFrame:(CGRect)frame {
+ if((self = [super initWithFrame:frame])) {
+ self.contentMode = UIViewContentModeRedraw;
+ }
+
+ return self;
+}
+
+- (void)drawRect:(CGRect)rect {
+ [(ABTableViewCell *)[self superview] drawContentView:rect highlighted:NO];
}
@end
+@implementation ABTableViewSelectedCellView
+
+- (id)initWithFrame:(CGRect)frame {
+ if((self = [super initWithFrame:frame])) {
+ self.contentMode = UIViewContentModeRedraw;
+ }
+
+ return self;
+}
+
+- (void)drawRect:(CGRect)rect {
+ [(ABTableViewCell *)[self superview] drawContentView:rect highlighted:YES];
+}
+
+@end
@implementation ABTableViewCell
-- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier
-{
- if(self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier])
- {
+- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
+ if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
contentView = [[ABTableViewCellView alloc] initWithFrame:CGRectZero];
contentView.opaque = YES;
- [self addSubview:contentView];
+ self.backgroundView = contentView;
[contentView release];
+
+ selectedContentView = [[ABTableViewSelectedCellView alloc] initWithFrame:CGRectZero];
+ selectedContentView.opaque = YES;
+ self.selectedBackgroundView = selectedContentView;
+ [selectedContentView release];
+
}
+
return self;
}
-- (void)dealloc
-{
+- (void)dealloc {
[super dealloc];
}
-- (void)setFrame:(CGRect)f
-{
+- (void)setSelected:(BOOL)selected {
+ [selectedContentView setNeedsDisplay];
+
+ if(!selected && self.selected) {
+ [contentView setNeedsDisplay];
+ }
+
+ [super setSelected:selected];
+}
+
+- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
+ [selectedContentView setNeedsDisplay];
+
+ if(!selected && self.selected) {
+ [contentView setNeedsDisplay];
+ }
+
+ [super setSelected:selected animated:animated];
+}
+
+- (void)setHighlighted:(BOOL)highlighted {
+ [selectedContentView setNeedsDisplay];
+
+ if(!highlighted && self.highlighted) {
+ [contentView setNeedsDisplay];
+ }
+
+ [super setHighlighted:highlighted];
+}
+
+- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
+ [selectedContentView setNeedsDisplay];
+
+ if(!highlighted && self.highlighted) {
+ [contentView setNeedsDisplay];
+ }
+
+ [super setHighlighted:highlighted animated:animated];
+}
+
+- (void)setFrame:(CGRect)f {
[super setFrame:f];
CGRect b = [self bounds];
- b.size.height -= 1; // leave room for the seperator line
+ // b.size.height -= 1; // leave room for the seperator line
[contentView setFrame:b];
+ [selectedContentView setFrame:b];
}
-- (void)setNeedsDisplay
-{
+- (void)setNeedsDisplay {
[super setNeedsDisplay];
[contentView setNeedsDisplay];
+
+ if([self isHighlighted] || [self isSelected]) {
+ [selectedContentView setNeedsDisplay];
+ }
+}
+
+- (void)setNeedsDisplayInRect:(CGRect)rect {
+ [super setNeedsDisplayInRect:rect];
+ [contentView setNeedsDisplayInRect:rect];
+
+ if([self isHighlighted] || [self isSelected]) {
+ [selectedContentView setNeedsDisplayInRect:rect];
+ }
+}
+
+- (void)layoutSubviews {
+ [super layoutSubviews];
+ self.contentView.hidden = YES;
+ [self.contentView removeFromSuperview];
}
-- (void)drawContentView:(CGRect)r
-{
+- (void)drawContentView:(CGRect)rect highlighted:(BOOL)highlighted {
// subclasses should implement this
}

1 comment on commit 87128d7

@vinod1988

nice custom cell

Please sign in to comment.
Something went wrong with that request. Please try again.