Permalink
Browse files

Now creating contentView, etc. on demand.

Not sure if the real thing does this or not, but it may. A subclass of
UITableViewCell that doesn't put stuff on the contentView directly but
instead just puts its own view on top of the cell seems to run into
problems with the contentView stealing taps/clicks unless it's made
on-demand and sorted properly. This may not be a good idea in the long
run but it seems to fix something for me at the moment.
  • Loading branch information...
1 parent c64f562 commit 254dc680cef114ed16faa6979691dd1d60fbfbcc @BigZaphod BigZaphod committed Jul 26, 2011
Showing with 47 additions and 22 deletions.
  1. +47 −22 UIKit/Classes/UITableViewCell.m
@@ -37,8 +37,8 @@
extern CGFloat _UITableViewDefaultRowHeight;
@implementation UITableViewCell
-@synthesize contentView=_contentView, accessoryType=_accessoryType, textLabel=_textLabel, selectionStyle=_selectionStyle, indentationLevel=_indentationLevel;
-@synthesize imageView=_imageView, editingAccessoryType=_editingAccessoryType, selected=_selected, backgroundView=_backgroundView;
+@synthesize accessoryType=_accessoryType, selectionStyle=_selectionStyle, indentationLevel=_indentationLevel;
+@synthesize editingAccessoryType=_editingAccessoryType, selected=_selected, backgroundView=_backgroundView;
@synthesize selectedBackgroundView=_selectedBackgroundView, highlighted=_highlighted, reuseIdentifier=_reuseIdentifier;
@synthesize editing = _editing, detailTextLabel = _detailTextLabel, showingDeleteConfirmation = _showingDeleteConfirmation;
@synthesize indentationWidth=_indentationWidth, accessoryView=_accessoryView;
@@ -53,20 +53,6 @@ - (id)initWithFrame:(CGRect)frame
_seperatorView = [[UITableViewCellSeparator alloc] init];
[self addSubview:_seperatorView];
- _contentView = [[UIView alloc] init];
- [self addSubview:_contentView];
-
- _imageView = [[UIImageView alloc] init];
- _imageView.contentMode = UIViewContentModeCenter;
- [_contentView addSubview:_imageView];
-
- _textLabel = [[UILabel alloc] init];
- _textLabel.backgroundColor = [UIColor clearColor];
- _textLabel.textColor = [UIColor blackColor];
- _textLabel.highlightedTextColor = [UIColor whiteColor];
- _textLabel.font = [UIFont boldSystemFontOfSize:17];
- [_contentView addSubview:_textLabel];
-
self.accessoryType = UITableViewCellAccessoryNone;
self.editingAccessoryType = UITableViewCellAccessoryNone;
}
@@ -100,28 +86,29 @@ - (void)layoutSubviews
{
[super layoutSubviews];
- CGRect bounds = self.bounds;
+ const CGRect bounds = self.bounds;
BOOL showingSeperator = !_seperatorView.hidden;
CGRect contentFrame = CGRectMake(0,0,bounds.size.width,bounds.size.height-(showingSeperator? 1 : 0));
- CGRect accessoryRect = CGRectMake(bounds.size.width, 0, 0, 0);
+ CGRect accessoryRect = CGRectMake(bounds.size.width,0,0,0);
+
if(_accessoryView) {
accessoryRect.size = [_accessoryView sizeThatFits: bounds.size];
accessoryRect.origin.x = bounds.size.width - accessoryRect.size.width;
accessoryRect.origin.y = round(0.5*(bounds.size.height - accessoryRect.size.height));
_accessoryView.frame = accessoryRect;
- if(_accessoryView.superview != self)
- [self addSubview: _accessoryView];
+ [self addSubview: _accessoryView];
contentFrame.size.width = accessoryRect.origin.x - 1;
}
_backgroundView.frame = contentFrame;
_selectedBackgroundView.frame = contentFrame;
_contentView.frame = contentFrame;
- [self bringSubviewToFront:_backgroundView];
- [self bringSubviewToFront:_selectedBackgroundView];
+ [self sendSubviewToBack:_selectedBackgroundView];
+ [self sendSubviewToBack:_backgroundView];
[self bringSubviewToFront:_contentView];
+ [self bringSubviewToFront:_accessoryView];
if (showingSeperator) {
_seperatorView.frame = CGRectMake(0,bounds.size.height-1,bounds.size.width,1);
@@ -141,6 +128,44 @@ - (void)layoutSubviews
}
}
+- (UIView *)contentView
+{
+ if (!_contentView) {
+ _contentView = [[UIView alloc] init];
+ [self addSubview:_contentView];
+ [self layoutIfNeeded];
+ }
+
+ return _contentView;
+}
+
+- (UIImageView *)imageView
+{
+ if (!_imageView) {
+ _imageView = [[UIImageView alloc] init];
+ _imageView.contentMode = UIViewContentModeCenter;
+ [self.contentView addSubview:_imageView];
+ [self layoutIfNeeded];
+ }
+
+ return _imageView;
+}
+
+- (UILabel *)textLabel
+{
+ if (!_textLabel) {
+ _textLabel = [[UILabel alloc] init];
+ _textLabel.backgroundColor = [UIColor clearColor];
+ _textLabel.textColor = [UIColor blackColor];
+ _textLabel.highlightedTextColor = [UIColor whiteColor];
+ _textLabel.font = [UIFont boldSystemFontOfSize:17];
+ [self.contentView addSubview:_textLabel];
+ [self layoutIfNeeded];
+ }
+
+ return _textLabel;
+}
+
- (void)_setSeparatorStyle:(UITableViewCellSeparatorStyle)theStyle color:(UIColor *)theColor
{
[_seperatorView setSeparatorStyle:theStyle color:theColor];

0 comments on commit 254dc68

Please sign in to comment.