TableViewCell scrambled when setting UILabel.text in cellForRowByIndexPath

Hi Folks! I've been struggling with an issue for some time, where when I generate new cells in a tableview, the cells are scrambled.


I assumed my cells wern't being dequeued properly, but having commented almost everything in the cellForRowAtIndexPath method, it worked fine.


Adding the code back line by line, it appers that the followling lines cause the problem.


UILabel *label;

label = (UILabel *)[cell viewWithTag:1];

label.text = [NSString stringWithFormat:@"%d", numerator[indexPath.row]];


I set the value of a UITextField in the same function without it causing a problem, so I'm somewhat baffled by this.


Is there a problem setting the label text in this function, or am I doing something else wrong?


Thank!


Screwtape.

Check out -(void)prepareForReuse. I'm guessing you're adding a label over and over when the cells are being reused. You'll need to clean out the label in prepareForReuse (or test to see if you already added a label to this cell, in cellForRowAtIndexPath: )

TableViewCell scrambled when setting UILabel.text in cellForRowByIndexPath
 
 
Q