user1529412 user1529412 - 3 months ago 14
iOS Question

CustomUITableViewCell not showing subviews on all Simulators

I have tableView with Custom cells. The custom cells have two buttons. The problem I am having is that the buttons in the cell don't show on all simulators, Actually they only show on iphone5 and ipad2 simulators but not the others. Any one has a clue?

Attached screenshots(iphone5 & iphone5S) and customCell code.

enter image description here
enter image description here

@interface AWSCameraProfileTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIButton *videoButton;
@property (weak, nonatomic) IBOutlet UIButton *uploadToCloudButton;
@end


@implementation AWSCameraProfileTableViewCell
@synthesize videoButton,uploadToCloudButton;


- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}


// This is the one that is typically called
// to set up our cell from the storyboard.
- (id) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];

if (self)
{
// Initialization code
self.cellID = 1;//cInvalidCellID;

// Install the custom background
self.tableViewCellBackgroundView = [TableViewCellBackgroundView newTableViewCellBackgroundViewWithFrame:self.frame
backgroundColor:[UIColor colorWithWhite:0.1
alpha:0.7]];
[self addSubview:self.tableViewCellBackgroundView];
[self sendSubviewToBack:self.tableViewCellBackgroundView];


NSLog(@"origin(%f,%f) Size(%f,%f)",self.contentView.bounds.origin.x,self.contentView.bounds.origin.y, self.contentView.bounds.size.height,self.contentView.bounds.size.width);

videoButton = [UIButton buttonWithType:UIButtonTypeCustom];
// [videoButton addTarget:self
// action:@selector(videoButtonClicked:)
// forControlEvents:UIControlEventTouchUpInside];
[videoButton setImage:[UIImage imageNamed:@"video.png"] forState:UIControlStateNormal];
videoButton.frame = CGRectMake(self.contentView.bounds.size.width- 40, 10.0, 36.0, 36.0);
[self.contentView addSubview:videoButton];

uploadToCloudButton = [UIButton buttonWithType:UIButtonTypeCustom];
// [uploadToCloudButton addTarget:self
// action:@selector(cloudButtonClicked:)
// forControlEvents:UIControlEventTouchUpInside];
[uploadToCloudButton setImage:[UIImage imageNamed:@"cloudUpload.png"] forState:UIControlStateNormal];
uploadToCloudButton.frame = CGRectMake(self.contentView.bounds.size.width- 40, 10.0, 36.0, 36.0);
[self.contentView addSubview:uploadToCloudButton];


}

return self;
}


- (id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reuseIdentifier];

if (self)
{
// Initialization code
self.cellID = -1; //cInvalidCellID;

// Install the custom background
self.tableViewCellBackgroundView = [TableViewCellBackgroundView newTableViewCellBackgroundViewWithFrame:self.frame
backgroundColor:[UIColor colorWithWhite:0.1
alpha:0.7]];
[self addSubview:self.tableViewCellBackgroundView];
[self sendSubviewToBack:self.tableViewCellBackgroundView];

[self addSubview:self.videoButton];
[self addSubview:self.uploadToCloudButton];
}

return self;
}


- (void) setCellID:(int)cellID
{
_cellID = cellID;
self.imageView.image = [UIImage imageNamed:@"DefaultCameraPreview100x100"],false;//[[CellManager shared] previewImageForCellID:cellID];
}


- (void)layoutSubviews
{
[super layoutSubviews];

float imageDiameter = self.frame.size.height * 100. / 120.;
self.imageView.bounds = CGRectMake(0, 0, imageDiameter, imageDiameter);

float inset = (self.frame.size.height - imageDiameter) / 2.0;

// Make iOS 7 look like iOS 6
// by performing explicit placement of image and label.
// This doesn't affect iOS 6.
CGRect imageViewFrame = self.imageView.frame;
imageViewFrame.origin.x = inset;
self.imageView.frame = imageViewFrame;

CGRect textLabelFrame = self.textLabel.frame;
textLabelFrame.origin.x = inset + imageDiameter + inset;
textLabelFrame.size.width = self.contentView.frame.size.width - self.frame.size.height;
self.textLabel.frame = textLabelFrame;

// Make sure the background view frame matches the cell view frame.
CGRect tableViewCellBackgroundViewFrame = self.tableViewCellBackgroundView.frame;
tableViewCellBackgroundViewFrame.size = self.frame.size;
self.tableViewCellBackgroundView.frame = tableViewCellBackgroundViewFrame;

videoButton.frame = CGRectMake(self.contentView.frame.size.width-40, 10.0, 36.0, 36.0);
uploadToCloudButton.frame = CGRectMake(self.contentView.frame.size.width-40, 10.0, 36.0, 36.0);

}


@end

Answer

You are placing these subviews using absolute values for self.contentView.bounds.size. You can't do that, because at the time this code runs, the cell has not yet been given its real size. Thus, you are ending up with the buttons off the screen.

Your best choice would be to position the subviews using autolayout. That way, you can pin them to the right edge of the cell, no matter what its size turns out to be.

Comments