简体   繁体   中英

how to set a imageview at center dynamically on tableview cell

I'm wondering how to set an imageview to the center of a tableview cell dynamically.

Here is my current code.

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100,100, 20, 20)];


imgView.image = [UIImage imageNamed:@"friendsCelluserpic.png"];


cell.imageView.image = imgView.image;

Try this code : This will align your image horizontally centre in TableViewCell

    float imgwidth=40;
    float imgheight=40;

    imgView = [[[UIImageView alloc] initWithFrame:CGRectMake((cell.contentView.bounds.size.width/2)-(imgwidth/2),15, imgwidth, imgheight)] autorelease];

    [cell.contentView addSubview:imgView];

try this, :)

UIImageView * imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"friendsCelluserpic.png"]];
imgView.frame=CGRectMake(100, 100, 20, 20);

[cell.contentView addSubview:imgView];
return cell;

你可以像这样编码[cell.contentView addSubview:imgView];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM