JoshDG
JoshDG

Reputation: 3931

xcode / cocoa: Reuse identifiers in custom cells

I have a tableview with custom cells via a uitableviewcell subclass. The cells have a label lblResult that changes after a a result is received. It works well, but, when I then scroll down my tableview, other random cells now have that same result label (but they still have their proper "name" label).

I figure this has to do with reuse identifiers, but I'm not sure.

Let me know if you need any more code to understand my problem.

The cell is made like this in cellForRowAtIndexPath

static NSString *CellIdentifier = @"tableCell";
FriendCell *cell =
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[FriendCell alloc]
             initWithStyle:UITableViewCellStyleDefault
             reuseIdentifier:nil];
}

Then before being returned, cell is passed into a loadingQueue dictionary. When the result loads there is a resultComplete method which loads the cell from the loadingQueue dictionary and the following is called:

   [[cell lblNumTagged] setText:[NSString stringWithFormat:@"(%d)",[thisDictionary count]]];
    [[cell lblNumTagged] setHidden:NO];

Upvotes: 1

Views: 1346

Answers (2)

Recycled Steel
Recycled Steel

Reputation: 2272

I know this is old but just in case. I think it is because when you create a new cell you are NOT specifying the identifier.

static NSString *CellIdentifier = @"tableCell";
FriendCell *cell =
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
   cell = [[FriendCell alloc]
         initWithStyle:UITableViewCellStyleDefault
         reuseIdentifier:CellIdentifier];
}

Try it like this, note the end of the last line.....

Upvotes: 0

Lefteris
Lefteris

Reputation: 14677

Yes it is because cells are being re-used.

In your custom cell class you can use the prepareForReuse delegate method, which is being called before the cell is being reused and clear the cell's label there...

Upvotes: 1

Related Questions