Reputation: 53
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];
}
Upvotes: 2
Views: 2288
Reputation: 53
I worked this one out in the end, I wasn't calling setNeedsDisplay on the cell's view, working great now.
Upvotes: 2
Reputation: 135548
One inconsistency I'm seeing: in tableView:cellForRowAtIndexPath:
you are calling -[StoreDetailFacilityCellView initWithFrame:reuseIdentifier:]
, which should be initWithStyle:reuseIdentifier:
.
Upvotes: 1