2011-12-01 67 views
0

當我的UITableView頂部和底部單元格每當他們滾動和返回到屏幕上時,我都遇到了問題。我認爲這是重複使用標識符的問題,但我不確定如何解決此問題。 PlaceTableViewCell是來自Loren Britcher的ABTableViewCell的子類,用於快速滾動。UITableView reuseIdentifier:頂部和底部的重複元素

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *uniqueIdentifier = @"Cell"; 

    PlaceTableViewCell *cell = (PlaceTableViewCell *)[tableView dequeueReusableCellWithIdentifier:uniqueIdentifier]; 


    if(cell == nil){ 
     cell = [[PlaceTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:uniqueIdentifier]; 
    } 

    Places *place = [self.placesArray objectAtIndex:[indexPath row]];  
    cell.place_title = place.title; 
    cell.place_street = place.street; 
    cell.place_thumb = place.thumb_path; 


    return cell; 

} 

編輯:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [self.placesArray count]; 
} 

從我ABTableViewCell的子類:

- (void)drawContentView:(CGRect)r 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    UIColor *backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]; 
    UIColor *textColor = [UIColor blackColor]; 

    if(self.selected) 
    { 
     backgroundColor = [UIColor clearColor]; 
     textColor = [UIColor whiteColor]; 
    } 

    [backgroundColor set]; 
    CGContextFillRect(context, r); 

    CGPoint p; 
    p.x = 42; 
    p.y = 2; 

     NSLog(@"place_thumb: %@", place_thumb); 

     NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:place_thumb]]; 
     UIImage *image = [UIImage imageWithData:imageData]; 
     [image drawAtPoint: p]; 


     p.x = p.x + 50; 

     [textColor set]; 
     [place_title drawAtPoint:p withFont:place_titleFont]; 

     p.y = 25; 

     textColor = [UIColor lightGrayColor]; 
     [textColor set]; 
    [place_street drawAtPoint:p withFont:place_streetFont]; 

} 
+0

你有什麼問題? – john

+0

如果向下滾動並滾動回頂部,則最後一個元素代替第一個元素。如果向上和向下滾動,則會發生相反的情況(第一個元素變爲最後一個元素) –

+0

它們全部位於同一節中嗎? – john

回答

0

cellForRowAtIndexPath看起來是正確的。你需要爲UITableView發佈所有的數據源方法。我有一個預感,這個問題可能發生在你的numberOfRows方法中。

您是否根據數組大小設置行數? 另外,你確定你的數組保持不變嗎?

+0

該數組保持不變......我還發布了drawContentView它我懷疑可能是問題的根源...可以在同一個線程上使用dataWithContentsOfURL加載的圖像是否會造成滯後,導致它執行此操作? –

+0

其實我試圖加載一個靜態圖像,而不是一個遠程圖像,問題仍然存在... –

+0

嗯,這是相當混亂。當您爲每個單元設置不同的標識符時會發生什麼?只是爲了進行實驗,使用indexPath.row作爲cellIdentifier。如果重複仍然存在,那麼我們可以排除表視圖的數據源方法成爲問題。 – Sid