有没有可能使复杂的子视图的tableview滚动像黄油一样平滑

时间:2013-02-05 18:31:43

标签: ios uitableview

在我的tableview中,我使用自定义单元格,它们上面有很多子视图,两个egoimageviews(imageview的子类,它接受一个URL并从网络中获取图像,然后将它们缓存供以后使用),带有链接检测的文本视图打开,4个标签和一个按钮。我在egoimageview和textview中添加了轻击手势。 textview的高度根据其保存的文本大小计算。高度计算很好地提前完成,因此当用户滚动时,由于高度计算,滚动性能不受影响。所有这些数据都从Web获取,然后计算文本高度和单元格高度并将其存储在数组中,然后将tableview添加为子视图。对于某些单元格,没有要显示的图像,因此在这些情况下,我只需在将其框架设置为cgrectzero后隐藏我的egoimageview。这些图像在iPhone屏幕上占据约170像素×100像素,每张约250 KB。当我滚动时,滚动是非常生涩的。我已经对慢滚动单元做了一些研究,到目前为止我已经实现了以下功能而没有显着的性能提升:

  1. 高度是提前计算好的,而不是高度计算方法。
  2. 单元格背景及其子视图的背景不透明。
  3. 数据布局有两种方式,所以我有两种类似的自定义单元类,但有一些差异,所以根据内容,要确定返回的单元格类型,尽管有90%的次,只使用了第一种。
  4. 我对这个生涩的卷轴并不是很满意,尽管有那么复杂的布局,但我一直在寻找能够让它滚动黄油的东西,但迄今为止没有任何帮助。请帮忙!

2 个答案:

答案 0 :(得分:0)

代码是

    - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if([type intValue] == POSTS) {
    Feed *feed = [postsArray objectAtIndex:indexPath.row];
    FeedCellFormattedDataObject *feedCellFormattedDataObject=[postsCellFormattedDataArray objectAtIndex:indexPath.row];
    if(feed.feedTypeIndex==0){//SIMPLEST CELL
        SimplestCell *simplestCell=[tableView dequeueReusableCellWithIdentifier:@"simplestcell"];
        if(!simplestCell){
            simplestCell=[[SimplestCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"simplestcell"];
            simplestCell.delegate=self;
            simplestCell.isInDetailedPostView=NO;
            simplestCell.selectionStyle = UITableViewCellSelectionStyleNone;
            [simplestCell.likeBtn addTarget:self action:@selector(likeButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        }
        simplestCell.cellIndex=indexPath.row;
        [simplestCell setDataWithFeed:feed andFeedCellFormattedDataObject:feedCellFormattedDataObject];
        return simplestCell;
    }
    if(feed.feedTypeIndex==1){//SIMPLEFEEDCELL WITH IMAGE
        SimpleFeedCell *simpleCellWithImage=[tableView dequeueReusableCellWithIdentifier:@"imagecell"];
        if(!simpleCellWithImage){
            simpleCellWithImage=[[SimpleFeedCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"imagecell"];
            simpleCellWithImage.delegate=self;
            simpleCellWithImage.isInDetailedPostView=NO;
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoOrVideoTapped:)];
            [simpleCellWithImage.feedImageView addGestureRecognizer:tapGesture];
            simpleCellWithImage.selectionStyle = UITableViewCellSelectionStyleNone;
            [simpleCellWithImage.likeBtn addTarget:self action:@selector(likeButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        }
        simpleCellWithImage.cellIndex=indexPath.row;
        [simpleCellWithImage setDataWithFeed:feed andFeedCellFormattedDataObject:feedCellFormattedDataObject];
        return simpleCellWithImage;
    }

    if(feed.feedTypeIndex==2){//SIMPLEFEEDCELL WITH VIDEO
        SimpleFeedCell *simpleCellWithVideo=[tableView dequeueReusableCellWithIdentifier:@"videocell"];
        if(!simpleCellWithVideo){
           simpleCellWithVideo=[[SimpleFeedCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"videocell"];
            simpleCellWithVideo.delegate=self;
            simpleCellWithVideo.isInDetailedPostView=NO;
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoOrVideoTapped:)];
            [simpleCellWithVideo.feedImageView addGestureRecognizer:tapGesture];
            simpleCellWithVideo.selectionStyle = UITableViewCellSelectionStyleNone;
            [simpleCellWithVideo.likeBtn addTarget:self action:@selector(likeButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
            simpleCellWithVideo.playVideoImageView.hidden=NO;
        }
        simpleCellWithVideo.cellIndex=indexPath.row;
        [simpleCellWithVideo setDataWithFeed:feed andFeedCellFormattedDataObject:feedCellFormattedDataObject];
        return simpleCellWithVideo;
    }

    if(feed.feedTypeIndex==3){//LINKFEEDCELL
        LinkFeedCell *linkFeedCell=[tableView dequeueReusableCellWithIdentifier:@"linkcell"];
        if(!linkFeedCell){
            linkFeedCell=[[LinkFeedCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"linkcell"];
            linkFeedCell.delegate=self;
            linkFeedCell.isInDetailedPostView=NO;
            linkFeedCell.selectionStyle = UITableViewCellSelectionStyleNone;
            [linkFeedCell.likeBtn addTarget:self action:@selector(likeButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

        }
        linkFeedCell.cellIndex=indexPath.row;
        [linkFeedCell setDataWithFeed:feed andFeedCellFormattedDataObject:feedCellFormattedDataObject];
        return linkFeedCell;
    }

}
else {
    EventCell * eventCell=(EventCell*)[tableView dequeueReusableCellWithIdentifier:@"eventcell"];
    if(!eventCell){
        eventCell=[[EventCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"eventcell"];
        eventCell.selectionStyle=UITableViewCellSelectionStyleGray;
    }
    [eventCell setCellDataWithEvent:[eventsArray objectAtIndex:indexPath.row]];
    return eventCell;
}

}

答案 1 :(得分:0)

设置数据的代码

 //set data and properties
self.dateLabel.text=feed.when;
self.msgTextView.text=feed.message;
self.likesCountLabel.text=feedCellFormattedDataObject.likesString;
self.commentsCountLabel.text=feedCellFormattedDataObject.commentsString;
self.userImageView.imageURL=feedCellFormattedDataObject.posterPicURL;
self.feedImageView.tag=self.cellIndex;
self.feedImageView.imageURL=feedCellFormattedDataObject.imageURL;
self.likeBtn.tag=self.cellIndex;
if(feed.canUserLike){
    self.likeBtn.hidden=NO;
    [self.likeBtn setBackgroundImage:feedCellFormattedDataObject.likeButtonImage forState:UIControlStateNormal];
}
else self.likeBtn.hidden=YES;

//adjust frames on the fly
self.msgTextView.frame=feedCellFormattedDataObject.msgTextViewFrame;
self.feedImageView.frame=feedCellFormattedDataObject.feedImageViewFrame;
self.likeBtn.frame=feedCellFormattedDataObject.likeBtnFrame;
self.likesCountLabel.frame=feedCellFormattedDataObject.likesCountLabelFrame;
self.commentsCountLabel.frame=feedCellFormattedDataObject.commentsCountLabelFrame;
self.commentsImageView.frame=feedCellFormattedDataObject.commentsImageViewFrame;
self.bgView.frame=feedCellFormattedDataObject.bgViewFrame;