Sonu Jha
Sonu Jha

Reputation: 719

is it possible to make a tableview with complex subviews scroll smooth like butter

In my tableview, I am using custom cells, which have many subviews on them, two egoimageviews (subclass of imageview that takes a URL and fetches images from the web and then caches them for later use), a textview with link detection turned on, 4 labels, and a button. I have added tap gestures to the egoimageview as well as the textview. The height of the textview is calculated as per the size of the text it holds. The height calculations are done well in advance, so that the scroll performance is not affected due to height calculation on the fly while the user scrolls. All this data is fetched from the web, then text heights and cell heights are calculated and stored in an array, before tableview gets added as a subview. For some cells, there are no images to display, so in those cases I simply hide my egoimageview after setting its frame to cgrectzero. The images occupy some 170 px X 100 px on the iphone screen, and are approximately 250 KB each. When i scroll, the scroll is quite jerky. I have done a bit of research on slow scrolling cells, and I have implemented the following so far without a significant performance improvement:

  1. Heights are calculated well in advance, not in heightforrow method.
  2. Cell backgrounds, and backgrounds of their subviews are opaque.
  3. There are two ways in which the data layout has to look like, so I have two similar kinds of custom cell classes with some differences, so as per the content, the cell type to return is decided, though 90% of the times, only the first kind is used.

I am not really satisfied with this jerky scroll, and have been looking up the web in frustration for something to get it scrolling butter smooth despite all that complex layout, but nothing has helped so far. Please help!

Upvotes: 0

Views: 210

Answers (2)

Sonu Jha
Sonu Jha

Reputation: 719

Code for setting data

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

Upvotes: 0

Sonu Jha
Sonu Jha

Reputation: 719

The code is

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

}

Upvotes: 0

Related Questions