多个自定义单元格的UITableView滚动不流畅

时间:2016-11-03 10:55:16

标签: ios objective-c uitableview vertical-scrolling

下面是索引路径中行的单元格的完整代码 - 此处表视图为多个自定义单元格,应根据值返回。滚动表视图滞后很多..我使用YYWebimage进行图像加载,并且单元格的内容不断变化,需要显示表格视图行高的高度,

- (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 个答案:

答案 0 :(得分:0)

您可以为自定义单元格使用子类。由于每行都有很多自定义,因此可以为每一行创建基本单元类的子类。

对于设置数据,您可以创建数据模型对象并创建行类的实例。然后你可以从cellforRowAtIndexpath设置数据模型,比如

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

还可以使用调度队列进行服务调用。

我希望它有所帮助!