Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Cells being created wrong due to reuse

I'm pretty sure the answer is in one of the "Build a Self Destructing Message - iPhone App" videos but I can't seem to find it.

Anyways, I have custom cells which have a button that is disabled depending on if a certain file on the back end exists. The problem is, as new cells are added to the view due to user interaction, they adopt the properties of the cell below them. I assume this is because the cell below them also happened to be the cell which existed at its index path, prior to the new cell being created.

Any idea on how to fix this?

1 Answer

Okay it seems to be solved now. For anyone having the same issue I fixed this by adding this method to my custom cell class:

-(void)prepareForReuse{

//Set all cell properties to default - in my case make the cell button selectable

}

Usually when you have a button in the cells and that button can change states based on a condition you should always cover both of the states with an if-else statement. Because of the reuse of the cells they tend to keep settings from one to the next and implementing that else statement can save you a lot of trouble.