ios - 在自定义 uitableViewCell 中获取错误值

标签 ios

大家好,我正在实现聊天 Messenger。我面临一个奇怪的情况。我有一个自定义的 tableview 单元格,当我正常滚动时它工作得很好。但是,如果我像疯子一样快速滚动,用户的图像就会混在一起。我想不出可能是什么原因,任何帮助伙伴和 thx 提前

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIColor *rung;
    if (indexPath.row%2 == 0) {
      //  0.662745 0.662745 0.662745
        rung=[UIColor colorWithRed:0.802745 green:0.802745 blue:0.802745 alpha:1];
        color=NO;
    }
    else {
       // 0.972549 0.972549 1
    //0.411765 0.411765 0.411765
        rung=[UIColor colorWithRed:0.862745 green:0.862745 blue:0.862745 alpha:1];
        color=YES;
    }



    //static NSString *MyIdentifier = @"Identifier";
    NSLog(@"====IndexPath is %d",indexPath.row);

    ChatSlideDC *slide = [[HMMainManager getSharedInstance].currentMeeting.resumedChatMessages objectAtIndex:indexPath.section];

    ChatMessageDC *message;

    message = [[slide ChatMessageArray]objectAtIndex:indexPath.row];
    NSLog(@"====Message Type is %@",message.MessageType);
    NSLog(@"%@",message.Message);
    if ([message.MessageType isEqualToString: @"0"] || [message.MessageType isEqualToString:@"1"]) {
        ChatCustomCell * cell = (ChatCustomCell *) [tableView dequeueReusableCellWithIdentifier:@"ChatCustomCell"];

        if (cell == nil) {

            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ChatCustomCell" owner:self options:nil];
            cell = (ChatCustomCell*)[nib objectAtIndex:0];
            }
    //    cell.backgroundImage.backgroundColor=rung;
        UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
        view.backgroundColor = rung;
        view.opaque = YES;
        cell.backgroundView = view;
        [view release];
        //cell.backgroundColor=rung;
        cell.lblSaid.text =@"Said";
        cell.lblName.text =message.SenderName;
        cell.lblText.text =message.Message;


        NSLog(@"manager strID: %@ chat cell attendeeID %@",[HMMainManager getSharedInstance].currentMeeting.strAttendeeID,message.AttendeeID);
        NSLog(@"%@ == %@",message.AttendeeID,[HMMainManager getSharedInstance].currentMeeting.strAttendeeID);
        if ([message.AttendeeID isEqualToString:[HMMainManager getSharedInstance].currentMeeting.strAttendeeID]) {
            cell.imgThumbUserPic.image = [MainManager getSharedInstance].userManager.userImage;
        }

        cell.selectionStyle = UITableViewCellEditingStyleNone;
   //     [message release];
        return cell;
    }
    if ([message.MessageType isEqualToString:@"2"] || [message.MessageType isEqualToString:@"3"] ) {
        if ([message.MessageType isEqualToString:@"3"] || (message.question && [message.question length]>0)) {
            //show asnwer cell
            AnswerCustomCell * cell = (AnswerCustomCell *) [tableView dequeueReusableCellWithIdentifier:@"AnswerCustomCell"];

            if (cell == nil) 
            {              
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AnswerCustomCell" owner:self options:nil];
                cell = (AnswerCustomCell*)[nib objectAtIndex:0];
            }
            UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
            view.backgroundColor = rung;
            view.opaque = YES;
            cell.backgroundView = view;
            [view release];
            //answer here goes
            cell.answerdBy.text = message.SenderName;
            cell.answerText.text = message.Message;
            [cell.answerText flashScrollIndicators];

            //question
            cell.questionedBy.text = message.QuestionBy;
            cell.questionText.text = message.question;
            [cell.questionText flashScrollIndicators];

            NSLog(@"%@",message.MessageId);

            NSLog(@"manager strID: %@ ans cell attendeeID %@",[HMMainManager getSharedInstance].currentMeeting.strAttendeeID,message.AttendeeID);


            if ([message.AttendeeID isEqualToString:[HMMainManager getSharedInstance].currentMeeting.strAttendeeID]) {
           //     if ([message.SenderName isEqualToString:compName] ) {



                 NSLog(@"----------:: %@ == %@ and message:%@",message.AttendeeID ,[HMMainManager getSharedInstance].currentMeeting.strAttendeeID,message.Message);
                cell.ansPic.image = [MainManager getSharedInstance].userManager.userImage;
           // }
            } 
            NSLog(@"manager strID: %@ ans cell attendeeID %@",[HMMainManager getSharedInstance].currentMeeting.strAttendeeID,message.QuestionID);

//            NSString *qustWalaID=[[HMMainManager getSharedInstance].currentMeeting.questionDict objectForKey:message.QuestionID];  
//            NSLog(@"%@",questionDict);
            if ([message.questionAttendeeID isEqualToString:[HMMainManager getSharedInstance].currentMeeting.strAttendeeID]) 
            {
        //        if ([message.QuestionBy isEqualToString:compName] ) {

                NSLog(@"------------:: %@ == %@ and message:%@",message.questionAttendeeID ,[HMMainManager getSharedInstance].currentMeeting.strAttendeeID,message.Message);

                cell.quePic.image = [MainManager getSharedInstance].userManager.userImage;
          //  }
            }
           // tempChatMessageDC=message;
          //  tempAnswerCustomCell=cell;
    //        [message release];
            return cell;
        }
        else {
            //show question cell

            QuestionCustomCell * cell = (QuestionCustomCell *) [tableView dequeueReusableCellWithIdentifier:@"QuestionCustomCell"];

            if (cell == nil) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"QuestionCustomCell" owner:self options:nil];
                cell = (QuestionCustomCell*)[nib objectAtIndex:0];
            }
            UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
            view.backgroundColor = rung;
            view.opaque = YES;
            cell.backgroundView = view;
            [view release];
            cell.lblSaid.text =@"Asked";
            cell.lblName.text =message.SenderName;

            NSLog(@"Question is %@", message.question);

            cell.lblText.text =message.Message;
            [cell.lblText flashScrollIndicators];
//            cell.lblAnswerQuestion.hidden = NO;
            if (!message.answered) {
                cell.lblAnswerQuestion.hidden = NO;
                cell.userInteractionEnabled = YES;
            }else {
                cell.lblAnswerQuestion.hidden = YES;
                cell.userInteractionEnabled = NO;
            }

//            cell.userInteractionEnabled = YES;
            [[HMMainManager getSharedInstance].currentMeeting.questionDict setObject:message.AttendeeID forKey:message.MessageId];  
            NSMutableDictionary *tmpDoct=[HMMainManager getSharedInstance].currentMeeting.questionDict ;
     //       [questionDict setObject:message.AttendeeID forKey:message.MessageId];
            NSLog(@"%@",tmpDoct );
            NSLog(@"manager strID: %@ ques cell attendeeID %@",[HMMainManager getSharedInstance].currentMeeting.strAttendeeID,message.AttendeeID);

            if ([message.AttendeeID isEqualToString:[HMMainManager getSharedInstance].currentMeeting.strAttendeeID]) {
                cell.imgThumbUserPic.image = [MainManager getSharedInstance].userManager.userImage;
            }

            cell.tvAnswer.delegate = self;

            NSMutableString* finalSec = [NSString stringWithFormat:@"%d",indexPath.section];

            for (NSInteger x=0; x<3; x++) {

                if (finalSec.length <3) {
                    finalSec = [NSString stringWithFormat:@"0%@",finalSec];
                }

            }

            NSLog(@"%@", finalSec);

            NSString* tag = [NSString stringWithFormat:@"%d%@",indexPath.row+1, finalSec];

             NSLog(@"tag value %d",[tag intValue]);

            cell.btnAnswer.tag = [tag intValue];//indexPath.row + 1000 + indexPath.section;
            cell.tvAnswer.tag = [tag intValue];
            cell.selectionStyle = UITableViewCellEditingStyleNone;
            [cell.btnAnswer addTarget:self action:@selector(answerButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    //        [message release];
            return cell;

        }

    }
 //   [message release];
    return nil;
}

最佳答案

你应该正确使用cell的reusing。你有代码片段:

if ([message.AttendeeID isEqualToString:[HMMainManager getSharedInstance].currentMeeting.strAttendeeID]) {
            cell.imgThumbUserPic.image = [MainManager getSharedInstance].userManager.userImage;
        } 

您不处理其他变体。在任何情况下你都应该设置用户图片

if ([message.AttendeeID isEqualToString:[HMMainManager getSharedInstance].currentMeeting.strAttendeeID]) {
            cell.imgThumbUserPic.image = [MainManager getSharedInstance].userManager.userImage;
        } else {
               cell.imgThumbUserPic.image = nil; //or set it with another image
}

关于ios - 在自定义 uitableViewCell 中获取错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12853398/

相关文章:

ios - parse.com key 类型无效,需要映射,但得到数组

ios - UIActivityIndi​​cator 在 NSFetchedResultsController 完成获取之前停止动画

ios - 在一个 .m 文件中无法识别 Google Analytics

ios - 将目标设置为 NSObject

ios - UICollectionView cellForItemAtIndexPath 滚动后返回 nil

ios - 在 iOS(Swift) 中管理 XML 数据

ios - 点击屏幕时的小延迟/抖动

ios - swiftUI 转换 : Scale from some frame - like iOS Homescreen is doing when opening an App

ios - 将苹果设备注册为开发者设备会对它做什么?

ios - 从iOS主屏幕启动的React Web App怪异行为