UITableViewCell的背景图像有时会发生变化

时间:2014-04-16 06:15:59

标签: ios objective-c uitableview

我有一个UITableView,它有一个后台UIImageView。很少有细胞将图像作为背景而其他细胞则没有。我面临的问题是,有时(在滚动和重复使用单元格时),错误的图像出现在错误的单元格上。在这种情况下,我将文本颜色更改为白色。在某些情况下,图像不会加载,但文本已经转换为白色。这导致普通单元格作为背景,文本颜色为白色。这不会一直发生。但它确实发生了。这就是我在cellForAtIndexPath中所做的事情:

// Image
    if ((NSNull*) imageString != [NSNull null])
    {
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:imageString]];
        [cell.questionImageView setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
            NSArray *array = [self.tableView indexPathsForVisibleRows];
            BOOL visible = NO ;
            for (NSIndexPath *ipath in array) {
                if (ipath.row == indexPath.row) {
                    visible = YES ;
                    break ;
                }
            }

            if (visible)
            {
                QuestFeedTableViewCell *recreateCell = (QuestFeedTableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];
                recreateCell.question.textColor = [UIColor whiteColor];
                recreateCell.questionImageView.contentMode = UIViewContentModeScaleAspectFill;
                UIColor *tintColor = [UIColor colorWithWhite:0.3 alpha:0.6];
                _postImage = image;
                recreateCell.questionImageView.alpha = 0.8;
                recreateCell.questionImageView.image = image;

                [recreateCell.questionImageView setClipsToBounds:YES];
                if (image)
                {
                    recreateCell.question.textColor = [UIColor whiteColor];
                    recreateCell.question.tintColor = [UIColor whiteColor];

                    recreateCell.answersCount.textColor = [UIColor whiteColor];
                    recreateCell.answersCount.tintColor = [UIColor whiteColor];
                }
                [recreateCell setNeedsLayout];

                [recreateCell.questionImageView setNeedsDisplay];
            }
        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
         {
             cell.questionImageView.image = nil;
             cell.question.textColor = [UIColor colorWithRed:77.0f/255.0f green:85.0f/255.0f blue:100.0f/255.0f alpha:1.0f];
             cell.question.tintColor = [UIColor colorWithRed:77.0f/255.0f green:85.0f/255.0f blue:100.0f/255.0f alpha:1.0f];
             [cell setNeedsDisplay];
         }];
    }
    else
    {
        cell.questionImageView.image = nil;
        cell.question.textColor = [UIColor colorWithRed:77.0f/255.0f green:85.0f/255.0f blue:100.0f/255.0f alpha:1.0f];
        cell.question.tintColor = [UIColor colorWithRed:77.0f/255.0f green:85.0f/255.0f blue:100.0f/255.0f alpha:1.0f];
        cell.answersCount.textColor = [QuestUtility colorFromHexString:@"#757b86"];
        cell.answersCount.tintColor = [QuestUtility colorFromHexString:@"#757b86"];
    }

这个问题不会一直发生。什么是最好的解决方案?为这两个创建单独的XIB?

3 个答案:

答案 0 :(得分:0)

你知道重用细胞机制。因此,有可能在单元格索引为1时获取图像,但单元格在索引为6时出列,例如,其背景已更改。

您应该将indexPath保留在成功块中,当成功块回调时,检查indexPath是否在tableview的indexPathsForVisibleRows数组中,如果是,则使用cellForRowAtIndexPath:indexPath方法获取单元格将图像分配给imageView和其他操作...

我有一个关于UICollectionView的项目示例,我认为这会有所帮助。在项目中,我使用UICollectionView而不是UITableView,但它在基础上是相同的。我在另一个线程上获取UIImage,然后将其分配给单元格的imageView。

    Image *image = (Image *)multimedium ;
    NSString *thumbnaiPath = image.thumbnailPath ;
    UIImage *imageObj = _thumbnailImageCache[thumbnaiPath] ;
    if (!imageObj) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            // I omit some time consuming work to get the data
            UIImage *thumbnail = [UIImage imageWithData:data] ;
            dispatch_async(dispatch_get_main_queue(), ^{
                _thumbnailImageCache[thumbnaiPath] = thumbnail ;
                NSArray *array = [_collectionView indexPathsForVisibleItems] ;
                BOOL visible = NO ;
                for (NSIndexPath *ipath in array) {
                    if (ipath.row == indexPath.row) {
                        visible = YES ;
                        break ;
                    }
                }
                if (visible) {
                    UICollectionViewCell *cell = [_collectionView cellForItemAtIndexPath:indexPath] ;
                    UIImageView *view = (UIImageView *)[cell.contentView viewWithTag:imageViewTag] ;
                    view.image = thumbnail ;
                }
            }) ;
        }) ;
    } else {
        imageView.image = imageObj ;
    }

答案 1 :(得分:0)

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

 if ((NSNull*) imageString != [NSNull null])
{
      cell.questionImageView.contentMode = UIViewContentModeScaleAspectFill;
     //get a dispatch queue

     dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

     //this will start the image loading in bg

    dispatch_async(concurrentQueue, ^{    

    NSData *image = [[NSData alloc] initWithContentsOfURL:imageString];

    //this will set the image when loading is finished
    dispatch_async(dispatch_get_main_queue(), ^{
        cell.questionImageView = [UIImage imageWithData:image];
       });
     });

}

}

您还可以关注以下网址“UITableView repeating cells when scrolled

答案 2 :(得分:0)

在viewWillAppear中分配init storeIndexArray

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *cellIdentifier = @"Cell Identifier";


TimeSheetCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];


if (!cell) {

    cell = [[TimeSheetCell alloc]initWithStyle:UITableViewCellStyleDefault
                                    reuseIdentifier:cellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryNone;

}

cell.selectionStyle = UITableViewCellSelectionStyleNone;

if ([self.storeIndexArray containsObject:indexPath])
{
    [cell.checkImage setImage:[UIImage imageNamed:@"check_box_selected.png"]];
}
else
{
    [cell.checkImage setImage:[UIImage imageNamed:@"check_box.png"]];
}


cell.timeSheetModel = [self.timeSheetListArray objectAtIndex:indexPath.row];

return cell;

}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

TimeSheetCell *cell = (TimeSheetCell*)[tableView cellForRowAtIndexPath:indexPath];

[self.storeIndexArray addObject:indexPath];

[cell.checkImage setImage:[UIImage imageNamed:@"check_box_selected.png"]];


}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath    *)indexPath {

TimeSheetCell *cell = (TimeSheetCell*)[tableView cellForRowAtIndexPath:indexPath];

[self.storeIndexArray removeObject:indexPath];

[cell.checkImage setImage:[UIImage imageNamed:@"check_box.png"]];


}