2

Based on Apple's TableViewSuite sample project I've taken the structure of number 5 there, to create custom drawn table cells. This is working great, and scrolling speed is fantastic now compared to when I was using nibs.

It has however introduced one bug that I can't figure out - if I scroll rapidly around on the screen, the cell contents seems to get mixed up and cells will be displaying content which should appear in another cell. Sometimes multiple cells will even display the same content. If I then tap and hold on the cell as if I was selecting it, it refreshes to the correct content again. I've verified that the correct cell is being passed the correct data from the array in the table's viewcontroller, so I'm guessing it's some graphical glitch.

Any help would be great, I can't work out what I've done different to the samples. Relevant code below:

View Controller cellforrowatindexpath

static NSString *CellIdentifier = @"FacilityCell";      
StoreDetailFacilityCell *cell = (StoreDetailFacilityCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[StoreDetailFacilityCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
[cell configureCell:[storeFacilities objectAtIndex:indexPath.row] atRow:indexPath];
return cell;

StoreDetailFacilityCell.m

@implementation StoreDetailFacilityCell
@synthesize storeDetailFacilityCellView;

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]) {
        CGRect tzvFrame = CGRectMake(0.0, 0.0, self.contentView.bounds.size.width, self.contentView.bounds.size.height);
        storeDetailFacilityCellView = [[StoreDetailFacilityCellView alloc] initWithFrame:tzvFrame];
        [self.contentView addSubview:storeDetailFacilityCellView];
    }
    return self;
}

-(void)configureCell:(NSDictionary *)storeFacility atRow:(NSIndexPath *)row {
    NSLog([NSString stringWithFormat:@"updating %@ at row %i",[storeFacility objectForKey:@"facilityKey"],row.row]);
    storeDetailFacilityCellView.storeFacility = storeFacility;
}
-(void)redisplay {
    [storeDetailFacilityCellView setNeedsDisplay];
}
-(void)dealloc {
    [storeDetailFacilityCellView release];
    [super dealloc];
}

StoreDetailFacilityCellView.m

@implementation StoreDetailFacilityCellView

@synthesize highlighted;
@synthesize editing;
@synthesize storeFacility;

-(id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.opaque = NO;
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

-(void)setHighlighted:(BOOL)lit {
    if (highlighted != lit) {
        highlighted = lit;  
        [self setNeedsDisplay];
    }
}
-(void)drawRect:(CGRect)rect {
    UIColor *mainTextColor = nil;
    UIFont *mainFont = [UIFont systemFontOfSize:14];
    if (self.highlighted) {
        mainTextColor = [UIColor whiteColor];
    } else {
        mainTextColor = [UIColor blackColor];
    }
    if (!self.editing) {
        CGPoint point;
        [mainTextColor set];
        point = CGPointMake(80, 9);
        [[storeFacility objectForKey:@"facilityTitle"] drawAtPoint:point withFont:mainFont];
    }
}

-(void)dealloc {
    [storeFacility release];
    [super dealloc];
}
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Tom
  • 53
  • 1
  • 6
  • check this thread: http://stackoverflow.com/questions/16425122/ios-cellforrowatindexpath-cells-are-getting-mixed-up – checai May 14 '14 at 05:28
  • check this thread http://stackoverflow.com/questions/16425122/ios-cellforrowatindexpath-cells-are-getting-mixed-up – checai May 14 '14 at 05:29

2 Answers2

2

I worked this one out in the end, I wasn't calling setNeedsDisplay on the cell's view, working great now.

Tom
  • 53
  • 1
  • 6
1

One inconsistency I'm seeing: in tableView:cellForRowAtIndexPath: you are calling -[StoreDetailFacilityCellView initWithFrame:reuseIdentifier:], which should be initWithStyle:reuseIdentifier:.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • Good spot, I've changed this back to match the sample but I've still got the same problem unfortunately. – Tom Nov 12 '09 at 22:10