TableViewCell的Nib文件中的视图大小

时间:2015-09-18 10:32:42

标签: ios uitableview frame calayer uinib

我想在nib文件中获得UIImageView宽度以使其成为圆圈,但我的错误值

这是我的代码:

- (void)viewDidLoad {
[super viewDidLoad];
   //....
   UINib *cellNib = [UINib nibWithNibName:@"User_Row" bundle:nil];
   [_Menu_Table registerNib:cellNib forCellReuseIdentifier:@"user_row"];

}

在tableview中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    User_Row *user_row = [tableView dequeueReusableCellWithIdentifier:@"user_row"];
    if (user_row == nil){
        NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"User_Row" owner:self options:nil];
        user_row = (User_Row *)[nibs objectAtIndex:0];
    }
    [user_row.user_image layoutIfNeeded];
    user_row.user_image.layer.borderWidth = 3;
    user_row.user_image.layer.borderColor = [UIColor colorWithRed:40/255 green:62/255 blue:38/255 alpha:1].CGColor;
    CALayer *cellImageLayer = user_row.user_image.layer;
    NSLog(@"width is %f", user_row.user_image.frame.size.width);
    [cellImageLayer setCornerRadius:user_row.user_image.frame.size.width/2];
    [cellImageLayer setMasksToBounds:YES];
    return user_row;
}

2 个答案:

答案 0 :(得分:0)

int imgHeight =  imageView.layer.frame.size.height;
int imgWidth = imageView.layer.frame.size.width;
NSLog(@"height of image %i",imgHeight);
NSLog(@"widht of image %i",imgWidth);

答案 1 :(得分:0)

通过替换:

解决了这个问题
[user_row.user_image layoutIfNeeded];

user_row.frame = CGRectMake(0, 0, self.view.frame.size.width, row_height);
[user_row layoutIfNeeded];

但我想要一个解释