简体   繁体   中英

Adding UIImageView to custom UITableView Cell

Is this a proper way or are there better solutions?

Thanks a lot!

Code (rowAtIndexPath)

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(500, 50, 20, 20)];
        imageView.image = [UIImage imageNamed:@"Icon.png"];
        [cell addSubview:imageView];

You should add your views to cell's contentView not cell's view. Here's an example:

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(500, 50, 20, 20)];
imageView.image = [UIImage imageNamed:@"Icon.png"];
[cell.contentView addSubview:imageView];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.size.height, 20, 20)];

imageView.image = [UIImage imageNamed:@"Icon.png"];
[cell.contentView addSubview:imageView]; 

It is correct one. for large list of UITableView. If you use [cell addSubview:] it will look like flickering for large list.

There is my code and it work well.Hope it helps you.

UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.size.height, 20, 20)];
imageView.image = [UIImage imageNamed:@"Icon.png"];
[cell.contentView addSubview:imageView];

For Swift 3

let imgView : UIImageView = UIImageView(frame: CGRect(x: 500, y: 50, width: 70, height: 70)) 
     //CALayer for rounded image 
        let imageCALayer : CALayer = imgView.layer
        imageCALayer.cornerRadius = 35
        imageCALayer.masksToBounds = true
        imgView.image = UIImage(string: "Icon.png")
        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