oe a
oe a

Reputation: 2280

UITableView displaying 'phantom cell'

In certain cases in my tablecells, an image is added to the cell. My cells are not properly displaying. Here is a picture of what I mean:

enter image description here

The second lock icon doesn't belong there...im not sure how or why its ending up there. In this example, the array has a count of 2 so the additional lock is not even inside of a cell.

This never happens on the initial load but happens as soon as the cells are reloaded the first time. This is also confusing to me because the initial load function is the same as the reload function.

Here's the code for the cell itself:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [UITableViewCell configureFlatCellWithColor:[UIColor greenSeaColor] selectedColor:[UIColor wetAsphaltColor] reuseIdentifier:CellIdentifier inTableView:(UITableView *)tableView];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        [cell configureFlatCellWithColor:[UIColor greenSeaColor] selectedColor:[UIColor wetAsphaltColor]];

    }

    Room *room;

    //is data filtered?
    if(isFiltered)
    room = [filteredTableData objectAtIndex:indexPath.row];
    else
    room = [roomList objectAtIndex:indexPath.row];


    cell.textLabel.text = room.roomName;
    cell.detailTextLabel.text = room.hostUsername;

    //lock icon or not
  if(room.password != nil)
{
    UIImageView *pw = [[UIImageView alloc] initWithImage:img];
    pw.tag = [room.rid integerValue];
    [pw setImage:img];
    pw.frame = CGRectMake(cell.frame.size.width - cell.frame.size.height, cell.frame.origin.y,
                          cell.frame.size.height - 2, cell.frame.size.height - 2);
    [cell.contentView addSubview:pw];
}
else{ //remove the content view if not needed
    [[cell.contentView viewWithTag:[room.rid integerValue]] removeFromSuperview];
}


    return cell;
}

For the record, this does not happen iOS7 (I know it's under NDA but just wanted to mention that fact if it helps).

Upvotes: 1

Views: 241

Answers (2)

oe a
oe a

Reputation: 2280

I ended up creating my own UITableViewCell, initializing the image and used these two functions for the lock:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        img = [UIImage imageNamed:@"password.png"];                 
        pw = [[UIImageView alloc] initWithImage:img];
        [pw setImage:img];
        pw.frame = CGRectMake(self.frame.size.width - self.frame.size.height, self.frame.origin.y,
                              self.frame.size.height - 2, self.frame.size.height - 2);
        pw.tag = 1;

    }
    return self;
}
-(void)enableLock{
    [self.contentView addSubview:pw];
}

-(void)disableLock{
    [[self.contentView viewWithTag:1]removeFromSuperview];
}

Upvotes: 0

coneybeare
coneybeare

Reputation: 33101

The icon is not cleared on the reused cells, so remove the pw view if it doesn't have a password

if(room.password != nil)
{
    ...
    pw.tag = 12345;
} else {
    [[cell.contentView viewWithTag:12345] removeFromSuperView];
}

Of course, I would recommend you assign this view to the cell's accessoryView instead of doing it like you are now, but this answers your question. See the docs for more info on accessoryView

Upvotes: 2

Related Questions