UITableViewCell图像在滚动时发生变化 [英] UITableViewCell image changes when scrolling
问题描述
我有一个带有标题和每个单元格上的图像的UITableView.一些单元格将具有默认图像,而其他单元格则没有.当我滚动表格时,某些行的图像不是预期的,而是显示另一行的图像,而不是预期的图像.如果我不使用dequeuereuseidentifier,那么一切都会很好,但是我想使用它,因为我有很多单元格.
有什么建议吗?
<代码>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];if(cell == nil){单元格= [[UITableViewCell分配] initWithStyle:UITableViewCellStyleDefault重用标识符:@"MyCell"];CGRect titleRect = CGRectMake(60,6,200,24);UILabel * title = [[UILabel alloc] initWithFrame:titleRect];title.tag = 1;title.backgroundColor = [UIColor clearColor];title.font = [UIFont fontWithName:@"AdelleBasic-Bold" size:15.5];[cell.contentView addSubview:title];UIImageView * defaultCellImage = [[UIImageView分配] initWithFrame:CGRectMake(8,10,42,42)];defaultCellImage.tag = 2;[defaultCellImage setImage:[UIImage imageNamed:@"Default_Row_Image"]]];[cell.contentView addSubview:defaultCellImage];}NSUInteger行= [indexPath行];电影*电影= [_movies objectAtIndex:row];UILabel * titleRowLabel =(UILabel *)[cell.contentView viewWithTag:1];titleRowLabel.text = [电影标题];UIImageView * cellImage =(UIImageView *)[cell.contentView viewWithTag:2];如果(![movie.imageName isEqualToString:@"])[cellImage setImage:[UIImage imageNamed:[电影imageName]]]];返回单元}
将正确加载表视图中要使用的第一个单元格.由于没有要出队的单元格,因此 if(cell == nil)
将返回 YES
,并且您的单元格将其图像设置为默认图像.然后,如果稍后在该方法中满足您设置其他图像的条件,则将显示其他图像.到目前为止,一切都很好.
但是,当可重复使用的单元格出队时,它已经设置了映像,这可能不是默认设置.由于 cell == nil
现在将返回 NO
,因此即使应显示该图像,该单元也永远不会将其图像重置为默认图像.>
I have a UITableView with a title and an image on each cell. Some cells will have a default image and others will not. When I scroll the table, the image of some rows is not the expected and the image of another row gets displayed instead of the expected one. If I don't use dequeuereuseidentifier everything works fine, but I want to use it because I have lots of cells.
Any suggestion?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];
CGRect titleRect = CGRectMake(60, 6, 200, 24);
UILabel *title = [[UILabel alloc] initWithFrame: titleRect];
title.tag = 1;
title.backgroundColor = [UIColor clearColor];
title.font = [UIFont fontWithName:@"AdelleBasic-Bold" size:15.5];
[cell.contentView addSubview:title];
UIImageView *defaultCellImage = [[UIImageView alloc] initWithFrame:CGRectMake(8, 10, 42, 42)];
defaultCellImage.tag = 2;
[defaultCellImage setImage:[UIImage imageNamed: @"Default_Row_Image"]];
[cell.contentView addSubview:defaultCellImage];
}
NSUInteger row = [indexPath row];
Movie *movie = [_movies objectAtIndex: row];
UILabel *titleRowLabel = (UILabel *) [cell.contentView viewWithTag:1];
titleRowLabel.text = [movie title];
UIImageView *cellImage = (UIImageView *) [cell.contentView viewWithTag:2];
if (![movie.imageName isEqualToString:@""])
[cellImage setImage:[UIImage imageNamed: [movie imageName]]];
return cell;
}
The first cells to be used in your table view will be properly loaded. Since there is no cell to dequeue, the if (cell == nil)
will return YES
and your cell will have its image set to be the default. Then, if your condition for setting a different image is satisfied later in the method, a different image will be shown. So far, so good.
However, when a reusable cell is dequeued, it already has an image set, which might not be the default. Since cell == nil
will now return NO
, this cell will never have its image reset to the default one, even if it is the image that should be shown.
这篇关于UITableViewCell图像在滚动时发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!