2016-08-16 9 views
0

私はカスタムセルを持つtableViewを持っています。カスタムセルには2つのボタンがあります。私が抱えている問題は、セル内のボタンがすべてのシミュレータに表示されないということです。実際にはiphone5とipad2シミュレータでのみ表示されますが、他のものでは表示されません。誰にでも手掛かりがありますか?CustomUITableViewCellはすべてのシミュレータでサブビューを表示しません

添付スクリーンショット(iphone5 & iphone5S)とcustomCellコード。

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 

答えて

2

あなたはself.contentView.bounds.sizeの絶対値を使用して、これらのサブビューを配置しています。このコードが実行されるとき、セルはまだ実際のサイズが与えられていないので、これを行うことはできません。したがって、あなたは画面のボタンで終わります。

あなたは、autolayoutを使ってサブビューを配置することをお勧めします。そうすれば、そのサイズが何であるかにかかわらず、セルの右端にピンを固定することができます。

+0

私はストーリーボードからこれを行う必要がありますか?私の目標はまさにあなたが言いました、それらを右端に固定することです。 – user1529412

+0

あなたは両方のストーリーボードやコードを使って実行できます。 - [apple guide](https://developer.apple.com/library/prerelease/content/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html)を参照してください。プログラム的な制約を作成します。 – Yasir

+0

ありがとう - それは私が探していたものです。 – user1529412

関連する問題