简体   繁体   中英

UIImageView's intrinsic content size in UITableviewCell

I have a Tableview Cell with an Imageview. I want to show Image as full-size ie the height of image is same as actual image.

The Tableview cell height should be same as Actual image height.

How can I achieve this?

Thank you,

I assume you have an array having image names.

Use The following method

for Objective-C

  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
            UIImage *image = [UIImage imageNamed:[self.arrImages objectAtIndex:indexPath.row]];
            if (image == nil){
               return 36; //return any static value
            } else {
               return image.size.height;
            }
    }

for Swift

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    let image = UIImage(named: arrImages[indexPath.row])
    image?.size.height
    if image == nil {
        return 36  //return any static value
    } else {
        return (image?.size.height)!
    }

}

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