0

我想在 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];

我的问题是,当我滚动表格视图时,图像显示在其他单元格中。我做错了什么?

4

3 回答 3

1

可能发生的情况是,您不希望重复使用已经有图像的单元格。查看

a)当您重新使用一个单元格时,如果它有星形图像,则将其删除。

b)在返回单元格之前,在该点添加星号(使用任何测试来确定星号是否应该出现)。

于 2012-04-04T15:23:54.160 回答
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];
}

希望这可以消除您的图像相互显示的问题,谢谢:)

于 2012-06-11T08:00:37.187 回答
0

那些 UITableViewCells 被重用。在你的 cellForRowAtIndexPath 中查找

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

如果您只希望一个或一组单元格具有此特定布局,则可以为这些单元格传递不同的 cellIdentifier。

于 2012-04-04T15:22:16.017 回答