2012-03-12 47 views
0

我使用自定義表格視圖單元格和我的表格視圖顯示重複數據後索引5我不知道爲什麼它完成當在ipad它顯示索引8重複數據後它可能索引問題,但無法找到它下面是代碼 - (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath { 靜態的NSString * CellIdentifier = @ 「項目」;TableView顯示一些索引後重復的數據

ItemCell *cell = (ItemCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[ItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 
NSLog(@"indexpath.row is %d",indexPath.row); 

NSString *stre = [grouporphoto objectAtIndex:indexPath.row]; 
if([stre isEqualToString:@"Group"]){ 
    folderimage = YES; 
    NSLog(@"here is folder"); 

    cell.item = [items objectAtIndex:indexPath.row]; 


} 
else if([stre isEqualToString:@"Photo"]){ 
    folderimage = NO; 
    NSLog(@"here is Photo"); 

    cell.item = [items objectAtIndex:indexPath.row]; 


} 




return cell; 
} 

請幫助 在此先感謝

+1

格式化您的代碼plase :) – janusbalatbat 2012-03-12 09:13:10

+0

難道是你永遠不會進入ifs? 'cell.item'永遠不會被更新,這將解釋爲什麼你得到重複的數據(重用單元格)。把NSLog放在ifs之外,看它是否打印。 – lawicko 2012-03-12 09:31:46

回答

1

您應該重置其他else語句中的所有變量,以便當您的單元格被重用時,它們中的變量不是。

else 
{ 
    cell.item = nil; 
} 

這應該爲您解決它。

+0

謝謝Totumus,你說得對我用過這個其他{ cell = nil; cell = [[[ItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } – Jprat 2012-03-12 17:03:24

+0

常見至少在else語句之上添加代碼! – 2013-03-28 04:08:31

0

這是因爲iOS的處理TableViews的方式。一旦新的單元格出現(從視圖之外,即在屏幕底部之下),它將要求一個新的單元格,在這種情況下,它將嘗試重用已移出視圖的單元格。

由於重用,並非所有實例變量都在單元上重置。您需要明確更改您要更改的所有內容。換句話說,所有圖片,文字,標籤等等,你希望每個單元格可能不同,每次都明確地設置它們。

+0

謝謝尼科它適合我 – Jprat 2012-03-12 17:04:43