views:

56

answers:

1

I'm not sure if this is the correct way to create a UITableViewCell in code, but it works perfectly, until I need to reload the table as its returning old cells so not loading the new content.

- (UITableViewCell *)tableView:(UITableView *)tableViewData cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableViewData dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    if (indexPath.section == 0) {
        if ([marker objectForKey:@"imageUrl"]) {
            UIView *transparentBackground = [[UIView alloc] initWithFrame:CGRectZero];
            transparentBackground.backgroundColor = [UIColor clearColor];
            cell.backgroundView = transparentBackground;
            UIImageView *buildingView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 100, 100)];
            buildingView.image = [self imageWithImage:[ImageManipulator makeRoundCornerImage:[self loadImage:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.qut.edu.au%@", [marker objectForKey:@"imageUrl"]]]] :9 :9]];
            [cell addSubview:buildingView];
        } else {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 15)];
            label.text = @"test";
            [cell addSubview:label];
            [label release];
        }
    } else {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 15)];
        label.text = @"test";
        [cell addSubview:label];
        [label release];
    }
}
return cell;
}

Basically the cell that gets returned on line 3 is not nil (its the old one), therefore the if statement isn't fired and it returns the old cell. I could load a new cell each time but that will have issues with load and memory usage.

Whats the correct way to do this?

+1  A: 

It will be the old one as it's only (hopefully) created once, that's the point of queueing and dequeueing the reusable cell, to save memory. You'll have to just clear out any of the properties you need to.

Matt Williamson
But in other projects if I do [table reloadData] it would return a nil cell and hence create a new one. Instead this now returns an old cell
Rudiger
Crap, I've just realised what I've done. Thanks
Rudiger