3

I have this very serious problem of scrolling the table.

Initially i used GCD for loading the image in Background and setting on table cell. but the table was not scrolling smoothly. So i used SDWebImage for that but then the same thing is happening.

Could anyone let me know the reason for this. Why the table Scrolling is not smooth as expected.

Please let me know your views as my app is waiting its release for the only same purpose.

Code :

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

           NSString *CellIdentifier = @"Cell";
        customCellForExhibitor *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil)
        {
            NSArray *xibPath = [[NSBundle mainBundle]loadNibNamed:@"customCellForExhibitor" owner:self options:nil];
            for (id fileObject in xibPath)
            {
                cell = (customCellForExhibitor*)fileObject;
            }
        }

        objDataModel = [parserDataContentArray objectAtIndex:indexPath.section];

        cell.exhibitorNameLabel.text = [objDataModel exhibitorNameObjectClass];
        cell.exhibitorText.text = [objDataModel exhibitorOfferObjectClass];
        cell.exhibitorSponsorType.text = [objDataModel exhibitorSponsorTypeObjectClass];

        [cell.exhibitorSponsorType setTextAlignment:NSTextAlignmentRight];



//        #pragma mark GCD;
//    
//        NSString *ImageURL = [[parserDataContentArray objectAtIndex:indexPath.section] exhibitorImageObjectClass];
////        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
////        cell.exhibitorImage.image = [UIImage imageWithData:imageData];
//
//        dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//        //this will start the image loading in bg
//        dispatch_async(concurrentQueue, ^{
//        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
//        
//        //this will set the image when loading is finished
//        dispatch_async(dispatch_get_main_queue(), ^{
//            
//            cell.exhibitorImage.image = [UIImage imageWithData:imageData];
//            [cell setNeedsDisplay];
//
//                                                    });
//                                        });

     NSString *ImageURL = [[parserDataContentArray objectAtIndex:indexPath.section] exhibitorImageObjectClass];
     [cell.exhibitorImage setImageWithURL:[NSURL URLWithString:ImageURL]
                      placeholderImage:[UIImage imageNamed:@"placeholder.png"]];



        if ([cell.exhibitorSponsorType.text isEqualToString:@"Gold"]) {
            cell.exhibitorSponsorType.textColor = [UIColor colorWithRed:255/255.0 green:215/255.0 blue:0 alpha:1];

        }

        else if ([cell.exhibitorSponsorType.text isEqualToString:@"Silver"]){
            cell.exhibitorSponsorType.textColor = [UIColor colorWithRed:192/255.0 green:192/255.0 blue:192/255.0 alpha:1];
        }
        else cell.exhibitorSponsorType.textColor = [UIColor colorWithRed:229/255.0 green:228/255.0 blue:226/255.0 alpha:1];

        return cell;



}

Thank You Best Regards.

  • Please, edit with some code where you download and set this images inside your tableview – Lucas Eduardo Aug 07 '13 at 14:29
  • @LucasEduardo : Hey Please check the edited Question.Don't miss to see the comments part where i have used GCD. –  Aug 07 '13 at 14:32
  • @LucasEduardo : Do I need to use any table delegates or any SDWebImage delegates stuff ??? –  Aug 07 '13 at 14:36
  • No, that was supposed to be enough. In which device are you trying to run this code? Even with SDWebImages, this is heavy processation, for all image there's a new thread running in background, downloading stuff, updating the imageviews etc. – Lucas Eduardo Aug 07 '13 at 14:49
  • @LucasEduardo I am not sure but probably it is iPhone 4s. What do i do.Would you suggest me anything as this is kind of important to me and should be done ASAP. –  Aug 07 '13 at 14:51
  • Sorry, but I can't help you =/. Just try to remove any processation from `cellForRowAtIndexPath`if it possible. Maybe someone will have a better solution. Upvoted the question to help, good luck with you app – Lucas Eduardo Aug 07 '13 at 14:55
  • @LucasEduardo : Thank You. Would you let me know whether we can cache the text data as well in a similar fashion we did for the image –  Aug 07 '13 at 14:57

1 Answers1

0

Use lazy loading file instead here is the reference u can find it here along with demo how it is implemented.

https://stackoverflow.com/a/18032907/1305001

[cell addSubview:[self addViewWithURL:ImageURL NFrame:CGRectMake(0, 0, 50, 50)]];

add this method below cellForRow

-(UIView*)addViewWithURL:(NSString*)urlStr NFrame:(CGRect)rect
{
    LazyLoad *lazyLoading;

    lazyLoading = [[LazyLoad alloc] init];
    [lazyLoading setBackgroundColor:[UIColor grayColor]];
    [lazyLoading setFrame:rect];

    [lazyLoading loadImageFromURL:[NSURL URLWithString:urlStr]];
    return lazyLoading;
}

Set placeholder in LazyLoad.m file's init method as

-(id)init{
    if (self==[super init]) {
        [self setImage:[UIImage imageNamed:@"placeholder.png"]];
    }
    return self;
}

And change superClass of LazyLoad.h file to UIImageView from UIView as

@interface LazyLoad : UIImageView
Community
  • 1
  • 1
Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90