CustomUITableViewCell不显示所有模拟器上的子视图

时间:2016-08-16 23:23:48

标签: ios uitableview

我有自定义单元格的tableView。自定义单元格有两个按钮。我遇到的问题是单元格中的按钮没有显示在所有模拟器上,实际上它们只显示在iphone5和ipad2模拟器上,而不显示在其他模拟器上。任何人都有线索?

附加截图(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

1 个答案:

答案 0 :(得分:2)

您使用self.contentView.bounds.size的绝对值放置这些子视图。你不能这样做,因为在这段代码运行时,单元格还没有给出它的实际大小。因此,您最终会关闭屏幕上的按钮。

您最好的选择是使用autolayout定位子视图。这样,无论它的大小如何,您都可以将它们固定在单元格的右边缘。