2016-08-16 3 views
0

У меня есть tableView с пользовательскими ячейками. Пользовательские ячейки имеют две кнопки. Проблема, с которой я сталкиваюсь, заключается в том, что кнопки в ячейке не отображаются на всех тренажерах, на самом деле они отображаются только на симуляторах iphone5 и ipad2, но не на других. У кого есть ключ?CustomUITableViewCell не отображает subviews на всех симуляторах

скриншоты (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. Вы не можете этого сделать, потому что в момент запуска этого кода ячейка еще не получила реальных размеров. Таким образом, вы получаете кнопки с экрана.

Ваш лучший выбор - разместить позиции с помощью автовыключения. Таким образом, вы можете привязать их к правому краю ячейки, независимо от того, какой размер ее окажется.

+0

Должен ли я делать это из раскадровки? Моя цель - именно то, что вы упомянули, чтобы привязать их к правильному краю. – user1529412

+0

Вы можете сделать это как с помощью раскадровки, так и с помощью кода - попробуйте прочитать [apple guide] (https://developer.apple.com/library/prerelease/content/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html) о том, как создавать программные ограничения. – Yasir

+0

Спасибо, это то, что я искал. – user1529412

Смежные вопросы