2012-04-04 107 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]; 
} 

希望这样可以消除您的图像的问题越来越显示彼此,谢谢:)