TableView reloadData不会删除图像

时间:2014-05-27 07:06:59

标签: ios7 uitableview

我有tableView,有多种类型的动态原型单元格。当我在viewDidLoad中调用reloadData时,大多数数据都会重新加载,但图像会保留并与新内容重叠。还有另一个我失踪的命令吗?

这是代码段(为了简单起见,尝试编辑它):

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

PFObject *question = [self.friendsPosts objectAtIndex:indexPath.row];
...

else if ([[question objectForKey:@"questionType"] isEqualToString:@"PhotoChoice"]) {
    static NSString *CellIdentifier = @"PhotoChoiceCell";

    PhotoChoiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


    PFUser *asker = [question objectForKey:@"asker"];
    [asker fetch];

    cell.question = question;

    cell.usernameLabel.text =  asker.username;
    cell.questionLabel.text = [question objectForKey:@"questionText"];
    cell.delegate = self;

    [cell setCellIndexPath:indexPath];

    [cell.questionLabel adjustFontSizeToFit];

    cell.answered = false;


    NSDate *postedTime = [question createdAt];

    NSString *postedAgo = [postedTime dateTimeAgo];

    cell.timeLabel.text = postedAgo;

    [cell.likeButton setTitle:[NSString stringWithFormat:@"Likes (%@)", [question objectForKey:@"likes"]] forState:UIControlStateNormal];

    cell.commentButton.text = [NSString stringWithFormat:@"Comments (%lu)", (unsigned long)[[question objectForKey:@"comments" ] count]];



    PFRelation *allAnswerers = [question objectForKey:@"answerersRelation"];
    PFQuery *answerersQuery = [allAnswerers query];
    [answerersQuery whereKey:@"objectId" equalTo:[[PFUser currentUser] objectId]];
    [answerersQuery countObjectsInBackgroundWithBlock:^(int number, NSError *error) {
        if (error) {
            NSLog(@"Error %@ %@", error, [error userInfo]);
        }
        else {
            if (number == 0) {
                if ( [[question objectForKey:@"numPhotos"] isEqualToString:@"2" ]) {
                    [cell.photoButtonA1 setImage:allImages[0] forState:UIControlStateNormal];
                    [cell.photoButtonA2 setImage:allImages[1] forState:UIControlStateNormal];
                    cell.photoButtonA1.hidden = NO;
                    cell.photoButtonA2.hidden = NO;
                }
                else if ( [[question objectForKey:@"numPhotos"] isEqualToString:@"3"] ) {
                    [cell.photoButtonB1 setImage:allImages[0] forState:UIControlStateNormal];
                    [cell.photoButtonB2 setImage:allImages[1] forState:UIControlStateNormal];
                    [cell.photoButtonB3 setImage:allImages[2] forState:UIControlStateNormal];
                    cell.photoButtonB1.hidden = NO;
                    cell.photoButtonB2.hidden = NO;
                    cell.photoButtonB3.hidden = NO;
                }
                else if ( [[question objectForKey:@"numPhotos"] isEqualToString:@"4"] ) {
                    [cell.photoButtonC1 setImage:allImages[0] forState:UIControlStateNormal];
                    [cell.photoButtonC2 setImage:allImages[1] forState:UIControlStateNormal];
                    [cell.photoButtonC3 setImage:allImages[2] forState:UIControlStateNormal];
                    [cell.photoButtonC4 setImage:allImages[3] forState:UIControlStateNormal];
                    cell.photoButtonC1.hidden = NO;
                    cell.photoButtonC2.hidden = NO;
                    cell.photoButtonC3.hidden = NO;
                    cell.photoButtonC4.hidden = NO;
                }
            }

所以,我应该清楚地说明这些图像被设置为UIButtons。

1 个答案:

答案 0 :(得分:0)

好的,这是我的猜测:

您似乎有三种类型的单元格:一种是2张图片,一张是3张,另一张是4张;但它们都属于同一类并使用相同的单元标识符。 因此,当您回收(出列)您的表格单元格时,您可能会重新使用之前分配了4个图像的单元格,但这次将其用作2个图像单元格。因此,您只更新前4个图像中的2个图像,而其他两个图像保持不变。将少于4张图像设置为单元格(2或3)时,应将剩余图像设置为nil

虽然没有关于你的bug发生模式的进一步细节,但很难说。

尝试对您的代码进行此修改:

    // (previous code omitted)

    if (number == 0) {

            // Reset all cell images, regardless of type
            [cell.photoButtonA1 setImage:nil forState:UIControlStateNormal];
            [cell.photoButtonA2 setImage:nil forState:UIControlStateNormal];

            [cell.photoButtonB1 setImage:nil forState:UIControlStateNormal];
            [cell.photoButtonB2 setImage:nil forState:UIControlStateNormal];
            [cell.photoButtonB3 setImage:nil forState:UIControlStateNormal];

            [cell.photoButtonC1 setImage:nil forState:UIControlStateNormal];
            [cell.photoButtonC2 setImage:nil forState:UIControlStateNormal];
            [cell.photoButtonC3 setImage:nil forState:UIControlStateNormal];
            [cell.photoButtonC4 setImage:nil forState:UIControlStateNormal];
            //

            if ( [[question objectForKey:@"numPhotos"] isEqualToString:@"2" ]) {
                [cell.photoButtonA1 setImage:allImages[0] forState:UIControlStateNormal];
                [cell.photoButtonA2 setImage:allImages[1] forState:UIControlStateNormal];
                cell.photoButtonA1.hidden = NO;
                cell.photoButtonA2.hidden = NO;
            }
            else if ( [[question objectForKey:@"numPhotos"] isEqualToString:@"3"] ) {
                [cell.photoButtonB1 setImage:allImages[0] forState:UIControlStateNormal];
                [cell.photoButtonB2 setImage:allImages[1] forState:UIControlStateNormal];
                [cell.photoButtonB3 setImage:allImages[2] forState:UIControlStateNormal];
                cell.photoButtonB1.hidden = NO;
                cell.photoButtonB2.hidden = NO;
                cell.photoButtonB3.hidden = NO;
            }
            else if ( [[question objectForKey:@"numPhotos"] isEqualToString:@"4"] ) {
                [cell.photoButtonC1 setImage:allImages[0] forState:UIControlStateNormal];
                [cell.photoButtonC2 setImage:allImages[1] forState:UIControlStateNormal];
                [cell.photoButtonC3 setImage:allImages[2] forState:UIControlStateNormal];
                [cell.photoButtonC4 setImage:allImages[3] forState:UIControlStateNormal];
                cell.photoButtonC1.hidden = NO;
                cell.photoButtonC2.hidden = NO;
                cell.photoButtonC3.hidden = NO;
                cell.photoButtonC4.hidden = NO;
            }
        }
// (following code omitted)