Search code examples
iosuitableviewuiimageviewshow-hide

How to hide and unhide a imageview in a tableview cell?


I am using a tableview with two rows. Each row having a Label, Button and Image View .Initially the the row height is 50 with no image. When I click the Button in the cell the Image Picker will open and select a image from camera roll. So how can I update the tableview height to 100 with a Image View in cell?


Solution

  • You can achieve this using autolayout. If you design your cell like this in a xib :

    Cell for height 100 : enter image description here

    With these constraints on the image view :

    enter image description here

    It will look like this with height 50 :

    enter image description here

    The weird line is the image view that got automatically shrunk by autolayout. It looks bad in interface builder, but will be ok on the device/simulator. The one thing you have to remember about is to simply remove the image (not the image view!) when the cell is at 50 height and set it when it is at 100.

    Also I'd recommend you to learn about autolayout as it is a powerful tool.