nicklockwood / iCarousel

A simple, highly customisable, data-driven 3D carousel for iOS and Mac OS
http://www.charcoaldesign.co.uk/source/cocoa#icarousel
Other
12.01k stars 2.58k forks source link

Cant reuse -(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view #427

Closed Supertecnoboff closed 11 years ago

Supertecnoboff commented 11 years ago

Hi @nicklockwood

Sometimes I want to reload the carousel with different content, however it doesn't work.... the follow method does NOT get called again:

-(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view

Every time I want to make the carousel I call:

[self createIcarousel];

Which is the following function:

-(void)createIcarousel {

    NSLog(@"Carousel creation started!\n");

    _carousel = [[iCarousel alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
    _carousel.delegate = self;
    _carousel.dataSource = self;
    _carousel.type = iCarouselTypeCoverFlow2;
    _carousel.vertical = YES;
    //_carousel.userInteractionEnabled = YES;
    _carousel.centerItemWhenSelected = NO;
    [view_mainfeed addSubview:_carousel];

    [[topbar superview] bringSubviewToFront:topbar];
    [[likes superview] bringSubviewToFront:likes];
    [[comments superview] bringSubviewToFront:comments];
    [[b_1 superview] bringSubviewToFront:b_1];
    [[b_2 superview] bringSubviewToFront:b_2];
    [[b_3 superview] bringSubviewToFront:b_3];
    [[b_4 superview] bringSubviewToFront:b_4];
    [[b_5 superview] bringSubviewToFront:b_5];
    [[loading_progress superview] bringSubviewToFront:loading_progress];
    [[blurContainerView superview] bringSubviewToFront:blurContainerView];

    NSLog(@"Carousel creation ended!\n");
}

And that function DOES get called but it then does NOT call one of your carousel methods....

-(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {

    static NSString *CellIdentifier = @"Cell";
    CustomCell *cell = (CustomCell *)[jsonTable dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = [nib objectAtIndex: 0];

        cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"uiFeedletsnglass5.png"]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell-on.png"]];
    }

    NSLog(@"Working carousel.");

    if (loading_account_type == 1) {
        cell.username.text = [[[episodes objectAtIndex:index] valueForKey:@"from"] valueForKey:@"name"];
        cell.message_post.text = [[[episodes objectAtIndex:index] valueForKey:@"data"] valueForKey:@"message"];
    }

    else if (loading_account_type == 2) {

    }

    else if (loading_account_type == 3) {
        cell.username.text = [[[[episodes_2 objectAtIndex:index] valueForKey:@"contentDetails"] valueForKey:@"upload"] valueForKey:@"videoId"];
        cell.geo.text = [[[episodes_2 objectAtIndex:index] valueForKey:@"snippet"] valueForKey:@"title"];
        cell.message_post.text = [[[episodes_2 objectAtIndex:index] valueForKey:@"snippet"] valueForKey:@"description"];

        dispatch_queue_t downloadQueue = dispatch_queue_create("image downloader INSTA", NULL);
        dispatch_async(downloadQueue, ^{
            //NSURL *url_2 = [NSURL URLWithString:[[[episodes objectAtIndex:indexPath.row] valueForKey:@"user"] valueForKey:@"profile_picture"]];
            //NSData *data = [NSData dataWithContentsOfURL:url_2];

            NSURL *url_3 = [NSURL URLWithString:[[[[[episodes_2 objectAtIndex:index] valueForKey:@"snippet"] valueForKey:@"thumbnails"] valueForKey:@"default"] valueForKey:@"url" ]];
            NSData *data_2 = [NSData dataWithContentsOfURL:url_3];

            dispatch_async(dispatch_get_main_queue(), ^{

                //UIImage *image = [UIImage imageWithData:data];
                //[cell.profilepic setImage:image];

                UIImage *image_2 = [UIImage imageWithData:data_2];
                [cell.picture setImage:image_2];
            });
        });
    }

    else if (loading_account_type == 4) {

        NSLog(@"Entered LAT 4 in carousel MAKING method");

        likes.text = [[[[episodes objectAtIndex:index] valueForKey:@"likes"] valueForKey:@"count"] stringValue];
        comments.text = [[[[episodes objectAtIndex:index] valueForKey:@"comments"] valueForKey:@"count"] stringValue];

        cell.username.text = [[[episodes objectAtIndex:index] valueForKey:@"user"] valueForKey:@"username"];
        cell.geo.text = [[[episodes objectAtIndex:index] valueForKey:@"user"] valueForKey:@"full_name"];
        cell.type.text = [[episodes objectAtIndex:index] valueForKey:@"type"];

        dispatch_queue_t downloadQueue = dispatch_queue_create("image downloader YT", NULL);
        dispatch_async(downloadQueue, ^{
            NSURL *url_2 = [NSURL URLWithString:[[[episodes objectAtIndex:index] valueForKey:@"user"] valueForKey:@"profile_picture"]];
            NSData *data = [NSData dataWithContentsOfURL:url_2];

            NSURL *url_3 = [NSURL URLWithString:[[[[episodes objectAtIndex:index] valueForKey:@"images"] valueForKey:@"standard_resolution"] valueForKey:@"url"]];
            NSData *data_2 = [NSData dataWithContentsOfURL:url_3];

            dispatch_async(dispatch_get_main_queue(), ^{

                UIImage *image_cell_insta = [UIImage imageWithData:data];
                [cell.profilepic setImage:image_cell_insta];

                UIImage *image_2 = [UIImage imageWithData:data_2];
                [cell.picture setImage:image_2];
            });
        });
    }

    else if (loading_account_type == 5) {

    }

    else if (loading_account_type == 6) {

    }

    cell.message_post.clipsToBounds = YES;
    cell.username.clipsToBounds = YES;
    cell.profilepic.clipsToBounds = YES;
    cell.geo.clipsToBounds = YES;
    cell.picture.clipsToBounds = YES;
    cell.contentView.clipsToBounds = NO;
    cell.clipsToBounds = YES;

    //cell.userInteractionEnabled = YES;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped_cell:)];
    [cell addGestureRecognizer:tap];

    return cell;
}
danielsonchris commented 11 years ago

Can you try testing by calling the [_carousel reloadData] method? That should cause the data to reload and if you change some of the state in your variables that might cause more data to be incorporated. Just a quick idea...

You will also probably need to remove the _carousel from the SubView (if it was previously added) if you are going to keep on calling your method [self createIcarousel];

I'll leave this here for record, but I don't think I'm helping your issue here. Sorry about that.

nicklockwood commented 11 years ago

As @danielsonchris says, you can reload the carousel contents by calling the reloadData method. There's no need to remove the carousel from the screen and re-create it.

Supertecnoboff commented 11 years ago

@nicklockwood @danielsonchris That worked perfectly, thank you so much! You guys have really helped my out here :)