0

My app has a UITableViewController. I want to show an image in the first section and add some other cells in another section. Like this:

-------
|     |   <--- image
|     |
-------

blah blah blah   <--- some other cells
--------------
blah blah blah
--------------
blah blah blah

And I thought it is as easy as this:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.section == irrelevant {
        let cell = UITableViewCell()
        let image = UIImageView(image: someImage, highlightedImage: nil)
        cell.contentView.addSubview(image)
        return cell
    }
    // Code for the other cells, irrelevant
}

But it came out like this:

enter image description here

As you can see, the image is out of the cell! What I want to do is to stretch the cell so that the image is actually "inside" the cell. I tried this:

cell.sizeToFit()

and this:

cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.width, image.frame.height + 15)

but both didn't work (The result stays the same)!

Is there a way to stretch it? If there isn't, how can I display an image in a UITableViewController?

Note: Don't tell me to set the imageView property of the cell. It's too small!

Sweeper
  • 213,210
  • 22
  • 193
  • 313

1 Answers1

1

Use this

override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {

 if indexPath.section == 0 {
   return heightForYourImage
  }
  else if indexPath.section == 1 {
    return heightForYour labels
  }

}
Ajay Kumar
  • 1,807
  • 18
  • 27