简体   繁体   中英

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM