iPhone UITableView单元格被“卡住”并与快速滚动混合在一起

时间:2009-11-12 21:38:35

标签: iphone objective-c uitableview scroll

基于Apple's TableViewSuite sample project我在那里采用了数字5的结构,以创建自定义绘制的表格单元格。这是非常好用的,与我使用笔尖时相比,滚动速度非常棒。

然而,它引入了一个我无法弄清楚的错误 - 如果我在屏幕上快速滚动,则单元格内容似乎混淆了,单元格将显示应出现在另一个单元格中的内容。有时,多个单元格甚至会显示相同的内容。如果我然后点击并按住单元格,就像我选择它一样,它会再次刷新到正确的内容。我已经验证了正确的单元格正在从表格的viewcontroller中的数组传递正确的数据,所以我猜它是一些图形故障。

任何帮助都会很棒,我无法解决我对样本所做的不同。相关代码如下:

查看控制器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];
}

2 个答案:

答案 0 :(得分:2)

最后我把这个工作了,我没有在单元格视图上调用setNeedsDisplay,现在工作得很好。

答案 1 :(得分:1)

我看到的一个不一致之处:在tableView:cellForRowAtIndexPath:您正在呼叫-[StoreDetailFacilityCellView initWithFrame:reuseIdentifier:],该initWithStyle:reuseIdentifier:应为{{1}}。