2009-11-12 75 views
2

基於Apple's TableViewSuite sample project我採用了5號結構,以創建自定義繪製表格單元格。這個工作很棒,和我使用筆尖時相比,滾動速度非常棒。iPhone UITableView單元變得「卡住」並與快速滾動混淆

但是它已經推出了一個錯誤,我想不通 - 如果我在屏幕上快速滾動左右,細胞內容物似乎弄混和細胞將展示其應該出現在另一個單元格的內容。有時多個單元甚至會顯示相同的內容。如果我點擊並按住單元格就好像選擇它一樣,它會再次刷新到正確的內容。我已經驗證了正確的單元格正在傳遞來自表的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]; 
} 
+0

檢查這個線程: http://stackoverflow.com/questions/16425122/ios-cellforrowatindexpath-cells-are-getting-mixed-up –

+0

檢查這個線程 http://stackoverflow.com/questions/ 16425122/ios-cellforrowatindexpath-cells-getting-getting-mixed-up –

回答

1

我看到一個不一致的情況:在tableView:cellForRowAtIndexPath:中,您打電話-[StoreDetailFacilityCellView initWithFrame:reuseIdentifier:],應該是initWithStyle:reuseIdentifier:

+0

不錯的地方,我已經改變這回匹配樣本,但不幸的是我仍然有同樣的問題。 – Tom

2

我工作這麼一出到底,我是不是該單元格的視圖中調用setNeedsDisplay,現在偉大的工作。