2012-04-04 36 views
0

我想在UITableViewCell的右側顯示圖像。 我使用這個代碼,做到這一點:UITableViewCell與右側圖像

CGRect imageRect = {80,0,225,44}; 
UIImageView *uiv = [[UIImageView alloc] initWithFrame:imageRect]; 
[uiv setImage:[UIImage imageNamed:[NSString stringWithFormat:@"star%@.png", [[self reataurant] valueForKey:@"stars"]]]]; 
[uiv setClipsToBounds:YES]; 
[cell addSubview:uiv]; 

我的問題是,該圖像是在其他細胞中所示,當我滾動tableview中。 我在做什麼錯了?

+0

的可能重複[HTTP://計算器的.com /問題/ 780395 /的UITableViewCell與 - 圖像 - 上的右(http://stackoverflow.com/questions/780395/uitableviewcell-with-image-on-the-right) – Sreeram 2012-04-04 15:17:51

回答

0

那些UITableViewCells被重用。看在你的cellForRowAtIndexPath爲

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

如果你只想要一個或一組特定的細胞具有這種特殊的佈局,可以通過不同的cellIdentifier這些細胞。

1

可能發生的情況是,如果您不希望重新使用已經有圖像的單元格,檢查

a)當您重新使用一個單元格時,如果它具有星形圖像,則將其刪除。 b)在您返回細胞之前,在該點添加恆星(無論您使用哪種測試來確定恆星是否應該出現)。

0

您可以輕鬆地將圖像視圖作爲附件視圖添加到表格單元格以獲得相同的效果。

這裏是你的理解的示例代碼:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fileName"]]; 
cell.accessoryView = imageView; 
[imageView release]; 

並確保您通過使用小區標識,以便覆蓋問題可以消除重複使用中的cellForRowAtIndexPath方法的表視圖的細胞空間;)

NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row]; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

希望這樣可以消除您的圖像的問題越來越顯示彼此,謝謝:)