0

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

  1. 高度是提前计算好的,而不是使用 heightforrow 方法。
  2. 单元格背景及其子视图的背景是不透明的。
  3. 数据布局有两种方式,所以我有两种相似的自定义单元格类,但有一些不同,所以根据内容,决定返回的单元格类型,虽然 90% 的时候,只有使用第一种。

我对这个生涩的滚动不太满意,尽管布局如此复杂,但我一直在沮丧地查找网络以使其滚动黄油平滑,但到目前为止没有任何帮助。请帮忙!

4

2 回答 2

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;
}

}

于 2013-02-06T17:55:05.333 回答
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;
于 2013-02-06T18:10:07.180 回答