nicklockwood / iCarousel

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

The resuingView in viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view is always nil #764

Closed olegbragin closed 7 years ago

olegbragin commented 7 years ago

Hi Nick, trying to implement that method, here it is:

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {
    CGRect frame = CGRectMake(0.0f, 0.0f, 260.0f, CGRectGetHeight(carousel.frame));
    TDriverView *driverView = nil;

    // создаем новое пресдтавление, потом его можно будет переиспользовать
    if (!view) {
        view = [[UIView alloc] initWithFrame:frame];
        driverView = [[TDriverView alloc] initWithFrame:frame];
        driverView.tag = index;

        [view addShadowToView:driverView withCornerRadius:defaultCornerRadius withShadowOpacity:0.15f];
        [view addSubview:driverView];
    }
    // переиспользуем ранее созданное представление
    else {
        driverView = (TDriverView *)[view viewWithTag:index];
    }

    driverView.delegate = self;
    [driverView updateWithOffer:self.offers[index]];

    return view;
}

I'm having several issues with it:

  1. the reusingView is always nil, so the "else" part never get called.
  2. After I call [carousel reloadData] I see weird results, described by you to many other people. But looking at my code, I'm sure I use the correct implementation got from examples.

Could help me to figure that out? Thanks in advance!

nicklockwood commented 7 years ago

Using the index as the tag definitely won't work. Use the same tag value for every driverView.

This line looks a little odd to me, but I assume this is category method you've added to UIView?

[view addShadowToView:driverView withCornerRadius:defaultCornerRadius withShadowOpacity:0.15f];

The rest seems OK to me.

olegbragin commented 7 years ago

Yep, that is a category method. I need a shadow only on parent view. Switched to use a constant tag = 1. Became a bit better, but still after reload seems something is broken. I see other people having a similar issue with view constraints, they're just gone probably. And still the resiingView is nil always, so I recreate a view every time. Is it because I have only 1 item in carousel for now?

olegbragin commented 7 years ago

I've figured out the markup issue. Turns out it has something o do with the Xcode 8 .xib file, Described here http://stackoverflow.com/questions/39578530/since-xcode-8-and-ios10-views-are-not-sized-properly-on-viewdidlayoutsubviews

olegbragin commented 7 years ago

Got everything working! thanks for the help! Turned out I needed to initialise the carousel later then viewDidLoad.