我想在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];
我的问题是,当我滚动桌面视图时,图像会显示在其他单元格中。 我错了什么?
答案 0 :(得分:1)
可能发生的情况是,当您不希望时,您正在重新使用已有图像的单元格。检查
a)当您重复使用单元格时,如果它有星形图像,则将其删除。
b)在你返回细胞之前,在那一点上添加星星(用你做的任何测试来确定星是否应该出现)。
答案 1 :(得分:0)
重用那些UITableViewCells。在你的cellForRowAtIndexPath中查找
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
如果您只想要一个或一组特定的单元格具有此特定布局,则可以为这些单元格传递不同的cellIdentifier。
答案 2 :(得分: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];
}
希望能消除你的图像相互显示的问题,谢谢:)