2016-11-03 3 views
0

Ниже приведен полный код для ячейки для строки по пути индекса. Здесь отображается таблица как несколько пользовательских ячеек, которые должны возвращаться на основе значений. Прокрутка вида таблицы запаздывает много .. Я использую YYWebimage для загрузки изображений и содержимое ячейки сохраняет изменения, на которых необходимо отобразить высоту табличной высоты строки,Прокрутка UITableView негладкая с несколькими настраиваемыми ячейками

- (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    KVembedLinkTableViewCell* KVCell = [tableView dequeueReusableCellWithIdentifier:@"Link"]; 

    [KVCell flushCell]; 
    [KVCell layoutIfNeeded]; 
    _postDict = [NSMutableDictionary dictionaryWithDictionary:[cardDetailsArray objectAtIndex:indexPath.row]]; 
    NSLog(@"feedstimeline postdict: %@",_postDict); 
    [_postDict setObject:indexPath forKey:KEY_INDEXPATH]; 
    NSArray *embedlist = [NSArray new]; 
    embedlist = [_postDict valueForKeyPath:@"recentpost.embedlist"]; 
    NSArray *medialist = [NSArray new]; 
    medialist = [_postDict valueForKeyPath:@"recentpost.medialist"]; 
    KVCell.embededDetails = _postDict; 
    KVCell.embedlist = embedlist; 

    NSString *discription = [NSString stringWithFormat:@"%@",[_postDict valueForKeyPath:@"recentpost.staticdata.contenttext"]]; 
    discription = [CVUtilityManager stripEmptyTags:discription]; 
    KVCell.userDescription.preferredMaxLayoutWidth = _timeLineTableView.bounds.size.width -16; 
    [KVCell.userDescription setText:discription]; 
    NSAttributedString *attr = [CVUtilityManager UserDescription:discription]; 
    KVCell.userDescriptionHeight.constant =[CVUtilityManager sizeForLabel:KVCell.userDescription withAttributedString:attr]; 
    [KVCell laodUserDescription:attr]; 
    [KVCell.userDescription updateConstraints]; 
    [KVCell.userDescription setNeedsLayout]; 
    [KVCell.userDescription layoutIfNeeded]; 
    [KVCell setLikeDelegate:self]; 
    NSString *typeOfCardString = cardTypeArray[indexPath.row]; 
    if ([CVUtilityManager stringIsNull:typeOfCardString]) typeOfCardString = @"UNKNOWN"; 
    _typeOfCard = [self defineCardTypeFromType:typeOfCardString]; 
    switch (_typeOfCard) { 

     case VIDEO: { 

      if (embedlist.count > 0) { 
       if (embedlist[0] != nil) { 
        [KVCell loadVideoDetails:[NSMutableDictionary dictionaryWithDictionary:embedlist[0]]]; 
       }else 
        KVCell.linkViewHeight.constant = 0; 
      }else 
       KVCell.linkViewHeight.constant = 0; 

      [KVCell performSelector:@selector(laodCellContents) onThread:[NSThread mainThread] withObject:nil waitUntilDone:TRUE]; 

     } break; 
     case UNKNOWN: 
     case TEXT: { 
      [KVCell loadTextWithString]; 
      KVCell.linkViewHeight.constant = 0; 
      KVCell.multiTableViewHeight.constant = 0; 
      [KVCell laodCellContents]; 
      [KVCell.linkContentView layoutIfNeeded]; 
      [KVCell.linkContentView updateConstraints]; 
      [KVCell layoutIfNeeded]; 
      [KVCell setNeedsLayout]; 
      [KVCell updateConstraints]; 
      NSLog(@"card type TEXT"); 
     }break; 
     case EMBEDFB: { 
      if (embedlist.count > 0) { 
       if (embedlist[0] != nil) { 
        NSString *URLFB = [embedlist[0] objectOrNilForKey:@"url"]; 
        [KVCell loadFaceBookDetails:[CVUtilityManager neutraliseString:URLFB] withVideoButtonStatus:NO]; 
       }else 
        KVCell.linkViewHeight.constant = 0; 
      }else 
       KVCell.linkViewHeight.constant = 0; 

      [KVCell performSelector:@selector(laodCellContents) onThread:[NSThread mainThread] withObject:nil waitUntilDone:TRUE]; 

     } break; 
     case EMBEDINSTAGRAM: { 

      if (embedlist.count > 0) { 
       if (embedlist[0] != nil) { 
        NSString *URLINSTA = [embedlist[0] objectOrNilForKey:@"url"]; 
        [KVCell loadInstagramDetails:[CVUtilityManager neutraliseString:URLINSTA]]; 
       }else 
        KVCell.linkViewHeight.constant = 0; 
      }else 
       KVCell.linkViewHeight.constant = 0; 

      [KVCell performSelector:@selector(laodCellContents) onThread:[NSThread mainThread] withObject:nil waitUntilDone:TRUE]; 

     } break; 

     case EMBEDPHOTO: { 

      if (embedlist.count > 0) { 
       if (embedlist[0] != nil) { 
        KVCell.linkViewHeight.constant = 253; 
        [KVCell embedPhoto:[NSMutableDictionary dictionaryWithDictionary:embedlist[0]]]; 

       }else 
        KVCell.linkViewHeight.constant = 0; 
      }else 
       KVCell.linkViewHeight.constant = 0; 
      KVCell.multiTableViewHeight.constant = 0; 
      KVCell.userDescription.preferredMaxLayoutWidth = _timeLineTableView.bounds.size.width -16; 
      [KVCell.userDescription setText:discription]; 
      NSAttributedString *attr = [CVUtilityManager UserDescription:discription]; 
      KVCell.userDescriptionHeight.constant =[CVUtilityManager sizeForLabel:KVCell.userDescription withAttributedString:attr]; 
      [KVCell laodUserDescription:attr]; 
      [KVCell.userDescription updateConstraints]; 
      [KVCell.userDescription setNeedsLayout]; 
      [KVCell.userDescription layoutIfNeeded]; 


      [KVCell performSelector:@selector(laodCellContents) onThread:[NSThread mainThread] withObject:nil waitUntilDone:TRUE]; 
     } break; 
     case KANVZPOST: 
     case LINK: { 

      if (embedlist.count > 0) { 
       if (embedlist[0] != nil) { 
        KVCell.linkViewHeight.constant = 253; 
        [KVCell loadLinkView:[NSMutableDictionary dictionaryWithDictionary:embedlist[0]]]; 

       }else 
        KVCell.linkViewHeight.constant = 0; 
      }else 
       KVCell.linkViewHeight.constant = 0; 
      KVCell.multiTableViewHeight.constant = 0; 

      [KVCell performSelector:@selector(laodCellContents) onThread:[NSThread mainThread] withObject:nil waitUntilDone:TRUE]; 

     } break; 
     case SOUND_CLOUD_AUDIO: 
     case MULTI_SOUND_CLOUD_AUDIO: { 
      NSLog(@"card type MULTI_SOUND_CLOUD_AUDIO"); 
      if (embedlist.count > 0) { 
       [KVCell loadAudioLinksWithArray:embedlist]; 
       KVCell.multiTableViewHeight.constant = embedlist.count * 132.00; 
       KVCell.linkViewHeight.constant = 0; 
      }else 
       KVCell.multiTableViewHeight.constant = 0; 
      KVCell.userDescription.preferredMaxLayoutWidth = _timeLineTableView.bounds.size.width -16; 
      [KVCell.userDescription setText:discription]; 
      NSAttributedString *attr = [CVUtilityManager UserDescription:discription]; 
      KVCell.userDescriptionHeight.constant =[CVUtilityManager sizeForLabel:KVCell.userDescription withAttributedString:attr]; 
      [KVCell laodUserDescription:attr]; 
      [KVCell.userDescription updateConstraints]; 
      [KVCell.userDescription setNeedsLayout]; 
      [KVCell.userDescription layoutIfNeeded]; 

      [KVCell performSelector:@selector(laodCellContents) onThread:[NSThread mainThread] withObject:nil waitUntilDone:TRUE]; 


     } break; 
     case EMBEDRICH: 
     case MULTIPLE_LINK: { 
      if (embedlist.count > 0) { 
       [KVCell loadMultiLinksWithArray:embedlist]; 
       KVCell.linkViewHeight.constant = 0; 
      }else 
       KVCell.linkViewHeight.constant = 0; 

      [KVCell performSelector:@selector(laodCellContents) onThread:[NSThread mainThread] withObject:nil waitUntilDone:TRUE]; 


     } break; 
     case FACEBOOK_VIDEO_CARD: { 
      if (embedlist.count > 0) { 
       if (embedlist[0] != nil) { 
        NSString *URLFB = [embedlist[0] objectOrNilForKey:@"url"]; 
        [KVCell loadFaceBookDetails:[CVUtilityManager neutraliseString:URLFB] withVideoButtonStatus:YES]; 

       }else 
        KVCell.linkViewHeight.constant = 0; 
      }else 
       KVCell.linkViewHeight.constant = 0; 

      [KVCell performSelector:@selector(laodCellContents) onThread:[NSThread mainThread] withObject:nil waitUntilDone:TRUE]; 

     } break; 
     case AUDIO: { 
      [_postDict setObject:indexPath forKey:KEY_INDEXPATH]; 
      KVembedAudio 
      * KVCellAUDIO = [tableView dequeueReusableCellWithIdentifier:@"Audio" forIndexPath:indexPath]; 
      KVCellAUDIO.embededDetails = _postDict; 
      [KVCellAUDIO setLikeDelegate:self]; 
      KVCellAUDIO.userDescription.preferredMaxLayoutWidth = _timeLineTableView.bounds.size.width -16; 
      [KVCellAUDIO.userDescription setText:discription]; 
      NSAttributedString *attr = [CVUtilityManager UserDescription:discription]; 
      KVCellAUDIO.userDescrptionHeight.constant =[CVUtilityManager sizeForLabel:KVCell.userDescription withAttributedString:attr]; 
      [KVCellAUDIO laodUserDescription:attr]; 
      [KVCellAUDIO.userDescription updateConstraints]; 
      [KVCellAUDIO.userDescription setNeedsLayout]; 
      [KVCellAUDIO.userDescription layoutIfNeeded]; 

      [KVCellAUDIO performSelector:@selector(laodCellContents) onThread:[NSThread mainThread] withObject:nil waitUntilDone:true]; 
      [KVCellAUDIO.contentView layoutIfNeeded]; 
      NSLog(@"card type AUDIO"); 
      return KVCellAUDIO; 
     } break; 
     case PHOTO: { 
      KVCell.embedlist = medialist; 
      if (medialist.count > 0) { 
       if (medialist[0] != nil) { 

        [KVCell uploadPhoto:[NSMutableDictionary dictionaryWithDictionary:medialist[0]]]; 

       }else 
        KVCell.linkViewHeight.constant = 0; 
      }else 
       KVCell.linkViewHeight.constant = 0; 
      KVCell.userDescription.preferredMaxLayoutWidth = _timeLineTableView.bounds.size.width -16; 
      [KVCell.userDescription setText:discription]; 
      NSAttributedString *attr = [CVUtilityManager UserDescription:discription]; 
      KVCell.userDescriptionHeight.constant =[CVUtilityManager sizeForLabel:KVCell.userDescription withAttributedString:attr]; 
      [KVCell laodUserDescription:attr]; 
      [KVCell.userDescription updateConstraints]; 
      [KVCell.userDescription setNeedsLayout]; 
      [KVCell.userDescription layoutIfNeeded]; 
      [KVCell performSelector:@selector(laodCellContents) onThread:[NSThread mainThread] withObject:nil waitUntilDone:TRUE]; 
     } break; 

     case MULTIPLE_EMBEDPHOTO: { 
      NSLog(@"card type MULTIPLE_EMBEDPHOTO"); 
      [_postDict setObject:indexPath forKey:KEY_INDEXPATH]; 
      NSArray *mediaList = [[_postDict objectOrNilForKey:@"recentpost"] objectOrNilForKey:@"embedlist"]; 
      KVembedPhotoMultiple 
      * KVCellPhoto = [tableView dequeueReusableCellWithIdentifier:@"Photo Multiple" forIndexPath:indexPath]; 
      [KVCellPhoto flushCell]; 
      KVCellPhoto.embededDetails = _postDict; 
      if (mediaList.count>0) { 
       [KVCellPhoto loadEmbedPhotos:mediaList]; 
      } 
      [KVCellPhoto setLikeDelegate:self]; 
      KVCellPhoto.userDescription.preferredMaxLayoutWidth = _timeLineTableView.bounds.size.width -16; 
      [KVCellPhoto.userDescription setText:discription]; 
      NSAttributedString *attr = [CVUtilityManager UserDescription:discription]; 
      KVCellPhoto.userDescrptionHeight.constant =[CVUtilityManager sizeForLabel:KVCell.userDescription withAttributedString:attr]; 
      [KVCellPhoto laodUserDescription:attr]; 
      [KVCellPhoto.userDescription updateConstraints]; 
      [KVCellPhoto.userDescription setNeedsLayout]; 
      [KVCellPhoto.userDescription layoutIfNeeded]; 

      [KVCellPhoto performSelector:@selector(laodCellContents) onThread:[NSThread mainThread] withObject:nil waitUntilDone:TRUE]; 
      [KVCellPhoto.contentView layoutIfNeeded]; 
      return KVCellPhoto; 
     } break; 

     case MULTIPLE_PHOTO: { 
      NSLog(@"card type MULTIPLE_PHOTO"); 
      [_postDict setObject:indexPath forKey:KEY_INDEXPATH]; 
      NSArray *mediaList = [[_postDict objectOrNilForKey:@"recentpost"] objectOrNilForKey:@"medialist"]; 
      NSLog(@"mediaList %@",mediaList); 
      KVembedPhotoMultiple 
      * KVCellPhoto = (KVembedPhotoMultiple *)[tableView dequeueReusableCellWithIdentifier:@"Photo Multiple" forIndexPath:indexPath]; 
      [KVCellPhoto flushCell]; 
      KVCellPhoto.embededDetails = _postDict; 
      if (mediaList.count>0) { 
       [KVCellPhoto loadUploadedPhotos:mediaList]; 
      } 
      [KVCellPhoto setLikeDelegate:self]; 
      KVCellPhoto.userDescription.preferredMaxLayoutWidth = _timeLineTableView.bounds.size.width -16; 
      [KVCellPhoto.userDescription setText:discription]; 
      NSAttributedString *attr = [CVUtilityManager UserDescription:discription]; 
      KVCellPhoto.userDescrptionHeight.constant =[CVUtilityManager sizeForLabel:KVCell.userDescription withAttributedString:attr]; 
      [KVCellPhoto laodUserDescription:attr]; 
      [KVCellPhoto.userDescription updateConstraints]; 
      [KVCellPhoto.userDescription setNeedsLayout]; 
      [KVCellPhoto.userDescription layoutIfNeeded]; 

      [KVCellPhoto performSelector:@selector(laodCellContents) onThread:[NSThread mainThread] withObject:nil waitUntilDone:TRUE]; 
      [KVCellPhoto.contentView layoutIfNeeded]; 
      return KVCellPhoto; 
     } break; 
     case EMBED_MUL_VIDEO: { 
      [_postDict setObject:indexPath forKey:KEY_INDEXPATH]; 
      KVembedVideoMultipleTableViewCell 
      * KVCellVideo = [tableView dequeueReusableCellWithIdentifier:@"Video Multiple" forIndexPath:indexPath]; 
      KVCellVideo.embededDetails = _postDict; 
      [KVCellVideo setLikeDelegate:self]; 
      [KVCellVideo performSelector:@selector(laodCellContents) onThread:[NSThread mainThread] withObject:nil waitUntilDone:TRUE]; 
      [KVCellVideo.contentView layoutIfNeeded]; 
      NSLog(@"card type EMBED_MUL_VIDEO"); 
      return KVCellVideo; 
     } break; 
     case EMBEDTWEET: 
     case EMBED_MUL_TWEET: { 
      NSLog(@"card type EMBED_MUL_TWEET"); 
      [_postDict setObject:indexPath forKey:KEY_INDEXPATH]; 
      KVembedTweetMultiple* KVCellTweet = [tableView dequeueReusableCellWithIdentifier:@"Tweet Multiple" forIndexPath:indexPath]; 
      [KVCellTweet setLikeDelegate:self]; 
      KVCellTweet.embededDetails = _postDict; 
      KVCellTweet.embedlist = embedlist; 
      [KVCellTweet.userDescription setText:discription]; 
      KVCellTweet.userDescriptionHeight.constant = [CVUtilityManager sizeForLabel:KVCell.userDescription withAttributedString:attr]; 
      [KVCellTweet laodUserDescription:attr]; 
      [KVCellTweet.userDescription updateConstraints]; 
      [KVCellTweet.userDescription layoutIfNeeded]; 

      [KVCellTweet loadTweets]; 

      [KVCellTweet performSelector:@selector(laodCellContents) onThread:[NSThread mainThread] withObject:nil waitUntilDone:TRUE]; 
      [KVCellTweet.contentView updateConstraints]; 
      [KVCellTweet.contentView layoutIfNeeded]; 
      [KVCellTweet layoutIfNeeded]; 
      [KVCellTweet updateConstraints]; 
      return KVCellTweet; 

     } break; 
     case EMBED_MUL_AUDIO: { 
      NSLog(@"card type EMBED_MUL_AUDIO"); 
      [_postDict setObject:indexPath forKey:KEY_INDEXPATH]; 
      KVembedMultiAudio* KVCellAudio = (KVembedMultiAudio*)[tableView dequeueReusableCellWithIdentifier:@"EmbedMultiAudio" forIndexPath:indexPath]; 
      KVCellAudio.embededDetails = _postDict; 
      [KVCellAudio setLikeDelegate:self]; 
      KVCellAudio.userDescription.preferredMaxLayoutWidth = _timeLineTableView.bounds.size.width -16; 
      [KVCellAudio.userDescription setText:discription]; 
      NSAttributedString *attr = [CVUtilityManager UserDescription:discription]; 
      KVCellAudio.userDescrptionHeight.constant =[CVUtilityManager sizeForLabel:KVCell.userDescription withAttributedString:attr]; 
      [KVCellAudio laodUserDescription:attr]; 
      [KVCellAudio.userDescription updateConstraints]; 
      [KVCellAudio.userDescription setNeedsLayout]; 
      [KVCellAudio.userDescription layoutIfNeeded]; 
      [KVCellAudio performSelector:@selector(laodCellContents) onThread:[NSThread mainThread] withObject:nil waitUntilDone:true]; 
      [KVCellAudio.contentView layoutIfNeeded]; 
      return KVCellAudio; 
     } 
      break; 

     default: 
      break; 
    } 
    [KVCell updateConstraints]; 
    [KVCell layoutIfNeeded]; 
    return KVCell; 
} 


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 

    return UITableViewAutomaticDimension; 

} 

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSNumber *height = [self.heightAtIndexPath objectForKey:indexPath]; 
    if(height) { 
     return height.floatValue; 
    } else { 
     return UITableViewAutomaticDimension; 
    } 
} 
+1

Перемещение вызова веб-сервисов и загрузки изображений в фоновый поток, чтобы ваш планшет плавно прокручивался. – Vinodh

+0

В вашем tableView есть много вещей: cellForRowAtIndexPath: method. Вы должны вернуть ячейку как можно быстрее. Эта статья будет полезна https://medium.com/ios-os-x-development/perfect-smooth-scrolling-in-uitableviews-fd609d5275a5#.v9pdjnqga –

+0

@OstapHorbach Большое спасибо ... но где установить данные для моя ячейка .. так как я использую несколько пользовательских ячеек - я не могу установить данные в методе ячейки willdisplay .. любые предложения – PunithBm

ответ

0

Вы можете использовать подклассы для ваша пользовательская ячейка. Поскольку у вас есть много настроек для каждой строки, вы можете подклассифицировать свой базовый класс ячейки для каждой строки.

Для настройки данных вы можете создать объект модели данных и создать экземпляр этого класса. Затем вы можете настроить модель данных из cellforRowAtIndexpath, СТГ как

CustomRowCell *rowCell = [[CustomRowCell alloc] init]; 
rowCell.dataSource = [yourDictionary objectForKey:@"key"]// yourArray[indexPath.row]; 

использовать также очередь отправки для обслуживания вызовов.

Надеюсь, это поможет!

+0

Привет, спасибо за ответ --- как я подклассифицирую пользовательскую ячейку ... я уже не сделал этого. – PunithBm

+0

Да, я вижу это сейчас. Как я уже сказал, вам нужно переместить все настройки в рамках метода подкласса, скажем, в методе init. –

Смежные вопросы