Add label to custom tableviewcell dynamically

Hi everyone,

i want to create a custom cell with this layout

img. label
img. label
img. label

and if there is a condition dynamically add to the cell an other img and label with the same layout.

in the init method of cell subclass I’ve setup all, but when scroll the table, the table view show wrong data because create only 3-4 cell and reuse them to show datas. Looking for in internet I’ve found the prepareForReuse() method. but I don’t know what write to check the condition and add or not the image and the label.

How can I fix it?

You might try using two different prototype cells. One with your custom cell layout, and the other that includes the additional image and label.

One thing to watch out for in your cellForRowAtIndexPath method - you need to be sure and store something in all the fields of the cell (even if it is a blank). If you don’t, a reused cell would show information from the previous time it was used.

Have fun!

@rcasey thank you for your answer but you give me an better idea. thank your suggest I’ve solve the problem in the method prepareForReuse(). in order, I’ve insert all image and all label I need and I’ve make some check if I need the last image and label. if not, I switch off the alpha value and set the constraints for the last image and label. in the method prepareForReuse, I’ve done the same thing removing first all constraints and reinstall them. and it’s work.

Then Thank you very much