caoimghgin
caoimghgin

Reputation: 629

dequeueReusableCellWithIdentifier forIndexPath

I have a custom/sub-classed UITableViewCell using NSLayoutConstraints. The left most UIImageView may or may not be populated with an image, and if not, the objects to right of this field will naturally shift to the left. Works well within the custom UITableViewCell, however I'll be populating the content with my custom UITableViewController. Unfortunately, following code will populate every row cell.dispatchedView with an image -- though I've trapped to not populate rows with anything.

This has something to do with the dequeueReusableCellWithIdentifier. Ideas?

static NSString *CellIdentifier = @"CellIdentifier";


- (void)viewDidLoad {
    [super viewDidLoad];

    [self.tableView registerClass:[NXGActiveUnitsCell class] forCellReuseIdentifier:CellIdentifier];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {

    NSDictionary *item = [self.model.dataSource objectAtIndex:indexPath.row];

    NXGActiveUnitsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if([[[item objectForKey:@"cfs_no"] clean] length] > 0) {
        cell.dispatchedView.image = [UIImage imageNamed:@"p.png"];
        NSLog(@">>>%@",[item objectForKey:@"cfs_no"] );
    } else {
        NSLog(@">>> i got nothing so I should not touch this row...");
    }

    cell.departmentIconView.image = [UIImage imageNamed:@"f.png"];
    cell.unitLabel.text = [item valueForKey:@"unit_id"];

    return cell;
}

Upvotes: 0

Views: 1063

Answers (1)

Martin R
Martin R

Reputation: 539705

I don't know if this already solves your problem, but you should definitely set

cell.dispatchedView.image = nil;

in the else-case, because cells are reused.

Upvotes: 2

Related Questions