2016-07-13 5 views
-2

テーブルビューがフリーズし、600以上の行が再読み込みされています。バックグラウンドスレッドでデータを取得していて、メインスレッドでテーブルをリロードしようとしていますが、問題は同じです。テーブルビューでUIがフリーズする

これはcellForRowで私のコード..です

  if (![currentUID isEqualToString:[chatDict valueForKey:@"sender"]]) { 

       identifier = @"onlyRightChat"; 

      }else{ 

       identifier = @"onlyLeftChat"; 

      } 

      UITableViewCell *cell ; 

      cell= [tableView dequeueReusableCellWithIdentifier:identifier ]; 

      NSString *userName; 

      if ([chatDict valueForKey:@"sender_last_name"]) { 

       userName = [NSString stringWithFormat:@"%@ %@",[chatDict valueForKey:@"sender_first_name"],[chatDict valueForKey:@"sender_last_name"]]; 

      }else{ 

       userName = [NSString stringWithFormat:@"%@",[chatDict valueForKey:@"sender_first_name"]]; 

      } 

      NSString *str ; 

      if (![currentUID isEqualToString:[chatDict valueForKey:@"sender"]]) { 

       str = [NSString stringWithFormat:@"%@:\n%@",userName,[[Utils sharedObject] convertUnicodeToEmoji:[chatDict valueForKey:@"text"]]]; 

      }else{ 

       str = [NSString stringWithFormat:@"You:\n%@",[[Utils sharedObject] convertUnicodeToEmoji:[chatDict valueForKey:@"text"]]]; 

      } 

      CGRect labelSize =[self getRectForString:str]; 

      if (cell == nil) { 

       cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]; 

      } 

      cell.selectionStyle = UITableViewCellSelectionStyleNone; 

      TTTAttributedLabel *lbl = (TTTAttributedLabel *)[cell.contentView viewWithTag:5111]; 

      UIView *lblBgView = (UIView *)[cell.contentView viewWithTag:5113]; 

      UIView *bgView = (UIView *)[cell.contentView viewWithTag:5115]; 

      UILabel *timeLbl = (UILabel *)[cell.contentView viewWithTag:5114]; 

      UIImageView *userImgView = (UIImageView *)[cell.contentView viewWithTag:5116]; 


      UILabel *onlineIcon = (UILabel *)[cell.contentView viewWithTag:onlineStatusTag]; 

      onlineIcon.layer.cornerRadius = onlineIcon.frame.size.height/2.0; 

      onlineIcon.clipsToBounds = YES; 



      lbl.backgroundColor = [UIColor clearColor]; 

      lbl.enabledTextCheckingTypes = NSTextCheckingTypeLink|NSTextCheckingTypePhoneNumber|NSTextCheckingTypeAddress; 

      lbl.delegate = self; 

      if (![currentUID isEqualToString:[chatDict valueForKey:@"sender"]]) { 

       lbl.linkAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithRed:91/255.0f green:135/255.0f blue:198/255.0f alpha:1.0] ,NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};//[UIColor colorWithRed:119/255.0f green:195/255.0f blue:66/255.0f alpha:1.0] 

      }else{ 

       lbl.linkAttributes = @{NSForegroundColorAttributeName:[UIColor colorWithRed:194/255.0f green:228/255.0f blue:255/255.0f alpha:1.0] ,NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};//[UIColor colorWithRed:119/255.0f green:195/255.0f blue:66/255.0f alpha:1.0] 



      } 

      if ([currentUID isEqualToString:[chatDict valueForKey:@"sender"]]) { 

       onlineIcon.hidden = YES; 

      }else{ 

       onlineIcon.hidden = NO; 

       NSString *status = [chatDict valueForKey:@"status"]; 

       if(status){ 

        if([status isEqualToString:@"online"]){ 

         onlineIcon.backgroundColor = [UIColor colorWithRed:81/255.0 green:217/255.0 blue:93/255.0 alpha:1.0]; 

        }else if([status isEqualToString:@"offline"]){ 

         onlineIcon.backgroundColor = [UIColor yellowColor]; 

        }else{ 

         onlineIcon.backgroundColor = [UIColor colorWithRed:214/255.0 green:103/255.0 blue:101/255.0 alpha:1.0]; 

        } 

       }else{ 

        onlineIcon.backgroundColor = [UIColor yellowColor]; 

       } 

      } 

      userImgView.layer.cornerRadius = userImgView.frame.size.height/2; 

      userImgView.clipsToBounds = YES; 

      [userImgView sd_setImageWithURL:[NSURL URLWithString:[chatDict valueForKey:@"sender_picture"]]placeholderImage:[UIImage imageNamed:@"default_avatar"]]; 


      UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userImageViewTap:)]; 

      tapGesture.numberOfTapsRequired = 1; 

      userImgView.userInteractionEnabled = YES; 

      [userImgView addGestureRecognizer:tapGesture]; 


      //    NSLog(@"cell.frame.size.width %f",cell.frame.size.width); 

      float width = cell.frame.size.width; 

      float height = cell.frame.size.height; 

      if (![currentUID isEqualToString:[chatDict valueForKey:@"sender"]]) 

      { 

       bgView.frame = CGRectMake(width- labelSize.size.width-31-20-35,0,labelSize.size.width+31+20,cell.frame.size.height); 

       lblBgView.frame = CGRectMake(0,10,bgView.frame.size.width-15,labelSize.size.height+10); 

       lbl.frame = CGRectMake(8, 0, lblBgView.frame.size.width-16, labelSize.size.height+10); 

       timeLbl.frame = CGRectMake(width-170,height-25, 120,fontSize); 

       timeLbl.textAlignment = NSTextAlignmentRight; 



      }else{ 

       bgView.frame = CGRectMake(35,0,labelSize.size.width+31+20,cell.frame.size.height); 

       lblBgView.frame = CGRectMake(15,10,bgView.frame.size.width-15,labelSize.size.height+10); 

       lbl.frame = CGRectMake(8, 0, lblBgView.frame.size.width-16, labelSize.size.height+10); 

       timeLbl.frame = CGRectMake(55,height-25, 120,fontSize); 

       timeLbl.textAlignment = NSTextAlignmentLeft; 

      } 

      NSDate* dateComment = [[Utils sharedObject] StringToDate:[chatDict valueForKey:@"created_at"] withFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"]; 

      timeLbl.text = [[Utils sharedObject] relativeTimestamp:dateComment]; 

      timeLbl.font = [UIFont fontWithName:@"Lato-Regular" size:fontSize-3.0]; 



      lblBgView.layer.cornerRadius = 8.0f; 

      lblBgView.clipsToBounds = YES; 

      lbl.layer.cornerRadius = 8.0f; 

      lbl.font = [UIFont fontWithName:@"Lato-Regular" size:fontSize]; 

      lbl.numberOfLines = 0; 

      lbl.clipsToBounds = YES; 

      if (str!=(id)[NSNull null]) 

      { 

       if (![currentUID isEqualToString:[chatDict valueForKey:@"sender"]]) { 

        lbl.text = str; 

       }else{ 

        NSDictionary *attrDict = @{ NSFontAttributeName : [UIFont fontWithName:@"Lato-Regular" size:fontSize], NSForegroundColorAttributeName : [UIColor colorWithRed:146/255.0 green:183/255.0 blue:201/255.0 alpha:1.0]}; 

        NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"You:\n" attributes: attrDict]; 

        NSDictionary *attrDict1 = @{ NSFontAttributeName : [UIFont fontWithName:@"Lato-Regular" size:fontSize]}; 

        NSMutableAttributedString *attrString1 = [[NSMutableAttributedString alloc] initWithString:[[Utils sharedObject] convertUnicodeToEmoji:[chatDict valueForKey:@"text"]] attributes: attrDict1]; 


        [attrString appendAttributedString:attrString1]; 

        lbl.text = str; 

        lbl.textColor = [UIColor whiteColor]; 

       } 

      }else{ 

       lbl.text= @""; 

      } 
      return cell; 
     } 

とメソッドから呼び出すリロード..

-(void)getChatTextFromLocalDB{ 

// [self showLoadingView]; 

NSString *databaseKey = [NSString stringWithFormat:@"%@-%@",[self.m_dictInfo valueForKey:@"rid"],[[NSUserDefaults standardUserDefaults] objectForKey:@"rid"]]; 

NSString *localDBPath=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/GroupChat.plist"]; 

NSMutableDictionary *root = [[[NSMutableDictionary alloc] initWithContentsOfFile:localDBPath] mutableCopy]; 

NSMutableArray *chatDictAry = [root objectForKey:databaseKey]; 

NSString *localDBPath1=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/GeoConnectChat.plist"]; 

NSMutableDictionary *root1 = [[[NSMutableDictionary alloc] initWithContentsOfFile:localDBPath1] mutableCopy]; 

NSMutableArray *temp = [root1 objectForKey:@"chatDeleted"]; 





NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"created_at" ascending:YES]; 

NSArray *sorters = [[NSArray alloc] initWithObjects:sorter, nil]; 

NSArray *sortedArray = [chatDictAry sortedArrayUsingDescriptors:sorters]; 

NSString *lastMsgRid; 

[chatDictAry removeAllObjects]; 

[chatDictAry addObjectsFromArray:sortedArray]; 

if(chatDictAry.count||[temp containsObject:databaseKey ]){ 

    [self.chatArray removeAllObjects]; 

    for (NSDictionary *dict in chatDictAry) { 



     if(!([g_Delegate.grpBlockByMeList containsObject:[dict valueForKey:@"sender"]]||[g_Delegate.grpBlockMeList containsObject:[dict valueForKey:@"sender"]])){ 

      [self.chatArray addObject:dict]; 

      if([dict valueForKey:@"rid"]) 

       lastMsgRid = [dict valueForKey:@"rid"] ; 

      [self.usersArray addObject:[dict valueForKey:@"sender"]]; 

     } 

    } 

    [root writeToFile:localDBPath atomically:YES]; 

    dispatch_async(dispatch_get_main_queue(), ^(void){ 

     [self hideLoadingView]; 

    }); 





    if (self.chatArray.count) { 





     if(tablePositionTop){ 

      CGSize beforeContentSize = self.chatTableView.contentSize; 

      [self.chatTableView reloadData]; 

      CGSize afterContentSize = self.chatTableView.contentSize; 

      CGPoint afterContentOffset = self.chatTableView.contentOffset; 

      CGPoint newContentOffset = CGPointMake(afterContentOffset.x, afterContentOffset.y + afterContentSize.height - beforeContentSize.height); 

      self.chatTableView.contentOffset = newContentOffset; 

     } 

     else{ 

      [self.chatTableView reloadData]; 

      int64_t delayInSeconds = 0.2f; 

      dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 

      dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 

       if(actualSelf){ 

        [self.chatTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.chatArray count]-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 

       } 

      }); 

     } 

     [self getUsersOnlineStatus]; 

     if (isFirstTime) { 

      isFirstTime = NO; 



       [self getRemainingListFromServerWithLastMsgId:lastMsgRid]; 



     }else{ 

      //   [self hideLoadingView]; 

     } 

    }else{ 

     [self hideLoadingView]; 

    } 

} 

else 

{ 

    if (isFirstTime) { 

     isFirstTime = NO; 

     [self showLoadingView]; 

     [self getAllMessages]; 

    } 

} 

}

+1

あなたのコードを共有してください –

+0

再利用可能なセルを使用していますか?post -tableView:cellfoRrowAtIndexPath:コード、あなたの問題を調べることができます。 –

+0

いくつかのコードを教えてください。 –

答えて

0

私はあなたがWWDC 2012、ビル同時のセッション211を見てお勧めユーザーインターフェースを作成し、そこに概念を適用します。これは、内容が独立して照会されレンダリングされるセルを特徴とします。

基本的な考え方は次のとおりです。
1. tableView:cellForRowAtIndexPathでは、セルがインスタンス化されます。
2.同じメソッドでは、セルを取り込むためのデータを取得する操作が作成され、辞書に格納されます。セルへの参照が操作に渡されます。操作には、セルにデータを挿入して辞書から操作を削除する完了ハンドラがあります。
3.メソッドからセルが返される前に、操作が操作キューに追加されます。
4. tableView:didEndDisplayingCell:forRowAtIndexPathで、画面外に移動したセルの操作がキャンセルされ、辞書から削除されます。

関連する問題