Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusing view in UIPickerView with iOS 7

EDIT 12-14-2015 : This problem is again frequently reported on iOS 7, iOS 8, iOS 9.... I think Apple want not of this control anymore, and should just forbid us to use it instead of let such a major bug

I use a UIPickerView with custom views loaded from a Xib. I was using the method

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row 
    forComponent:(NSInteger)component reusingView:(UIView *)view

from UIPickerViewDelegate to reuse my views.

With iOS 6, my UIPickerview works perfectly and my views are reused.

Since iOS 7, the reusingView is always nil, so I need to load the NIB for each row (I have 250 rows). My views are doing some treatments so it can take a little part of memory but x250 = big memory leak.

To be sure of what the problem is, I did a simple project with a UILabel :

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

    UILabel *label = (UILabel *)view;
    if(view == nil) {
        label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 76)];
    }
    else {
        NSLog(@"*******************************************************");
    }
    NSLog(@"Current inndex : %i",row);
    label.text = [NSString stringWithFormat:@"TITRE %i", row];
    return label;
}

With iOS 6, my views are correctly reused :

2013-12-17 14:27:25.395 EssaiReusedViewPicker[261:907] Current inndex : 220 2013-12-17 14:27:25.396 EssaiReusedViewPicker[261:907] ******************************************************* 2013-12-17 14:27:25.397 EssaiReusedViewPicker[261:907] Current inndex : 221 2013-12-17 14:27:25.404 EssaiReusedViewPicker[261:907] ******************************************************* 2013-12-17 14:27:25.407 EssaiReusedViewPicker[261:907] Current inndex : 222 2013-12-17 14:27:25.409 EssaiReusedViewPicker[261:907] ******************************************************* 2013-12-17 14:27:25.410 EssaiReusedViewPicker[261:907] Current inndex : 223 2013-12-17 14:27:25.411 EssaiReusedViewPicker[261:907] ******************************************************* 2013-12-17 14:27:25.412 EssaiReusedViewPicker[261:907] Current inndex : 224 2013-12-17 14:27:25.414 EssaiReusedViewPicker[261:907] ******************************************************* 2013-12-17 14:27:25.415 EssaiReusedViewPicker[261:907] Current inndex : 225 2013-12-17 14:27:25.420 EssaiReusedViewPicker[261:907] ******************************************************* 2013-12-17 14:27:25.424 EssaiReusedViewPicker[261:907] Current inndex : 227 2013-12-17 14:27:25.425 EssaiReusedViewPicker[261:907]

But with iOS 7, it's an all-new story... :

2013-12-17 14:36:09.555 EssaiReusedViewPicker[7209:70b] Current inndex : 0
2013-12-17 14:36:09.557 EssaiReusedViewPicker[7209:70b] Current inndex : 1
2013-12-17 14:36:09.557 EssaiReusedViewPicker[7209:70b] Current inndex : 2
2013-12-17 14:36:09.560 EssaiReusedViewPicker[7209:70b] Current inndex : 0
2013-12-17 14:36:09.560 EssaiReusedViewPicker[7209:70b] Current inndex : 1
2013-12-17 14:36:09.561 EssaiReusedViewPicker[7209:70b] Current inndex : 2
2013-12-17 14:36:09.563 EssaiReusedViewPicker[7209:70b] Current inndex : 0
2013-12-17 14:36:09.563 EssaiReusedViewPicker[7209:70b] Current inndex : 1
2013-12-17 14:36:09.564 EssaiReusedViewPicker[7209:70b] Current inndex : 2
2013-12-17 14:36:09.566 EssaiReusedViewPicker[7209:70b] Current inndex : 0
2013-12-17 14:36:09.567 EssaiReusedViewPicker[7209:70b] Current inndex : 1
2013-12-17 14:36:09.567 EssaiReusedViewPicker[7209:70b] Current inndex : 2
2013-12-17 14:36:09.568 EssaiReusedViewPicker[7209:70b] Current inndex : 1
2013-12-17 14:36:09.568 EssaiReusedViewPicker[7209:70b] Current inndex : 2
2013-12-17 14:36:09.569 EssaiReusedViewPicker[7209:70b] Current inndex : 0
2013-12-17 14:36:09.569 EssaiReusedViewPicker[7209:70b] Current inndex : 1
2013-12-17 14:36:14.172 EssaiReusedViewPicker[7209:70b] Current inndex : 3
2013-12-17 14:36:14.350 EssaiReusedViewPicker[7209:70b] Current inndex : 3
2013-12-17 14:36:14.351 EssaiReusedViewPicker[7209:70b] Current inndex : 2
2013-12-17 14:36:14.367 EssaiReusedViewPicker[7209:70b] Current inndex : 4
2013-12-17 14:36:14.667 EssaiReusedViewPicker[7209:70b] Current inndex : 4
2013-12-17 14:36:14.668 EssaiReusedViewPicker[7209:70b] Current inndex : 3
2013-12-17 14:36:14.700 EssaiReusedViewPicker[7209:70b] Current inndex : 5
2013-12-17 14:36:15.067 EssaiReusedViewPicker[7209:70b] Current inndex : 5
2013-12-17 14:36:15.067 EssaiReusedViewPicker[7209:70b] Current inndex : 4
2013-12-17 14:36:15.083 EssaiReusedViewPicker[7209:70b] Current inndex : 6
2013-12-17 14:36:15.367 EssaiReusedViewPicker[7209:70b] Current inndex : 6
2013-12-17 14:36:15.367 EssaiReusedViewPicker[7209:70b] Current inndex : 5
2013-12-17 14:36:15.400 EssaiReusedViewPicker[7209:70b] Current inndex : 7
2013-12-17 14:36:16.416 EssaiReusedViewPicker[7209:70b] Current inndex : 7

So what's wrong? iOS or ...?

like image 311
QLag Avatar asked Dec 17 '13 13:12

QLag


People also ask

What is the uipickerviewdatasource protocol?

The UIPickerViewDataSource protocol must be adopted by an object that mediates between a UIPickerView object and your application’s data model for that picker view. The data source provides the picker view with the number of components, and the number of rows in each component, for displaying the picker view data.

What is a uipickerview delegate?

The delegate of a UIPickerView object must adopt this protocol and implement at least some of its methods to provide the picker view with the data it needs to construct itself. Gets the number of components for the picker view.

How do I use uidatepicker in the clock app?

The UIDatePicker class uses a custom subclass of UIPickerView to display dates and times. To see an example, tap the add (“+”) button in the Alarm pane of the Clock app. You provide the data to be displayed in your picker view using a picker data source—an object that adopts the UIPickerViewDataSource protocol.

How does uidatepicker work?

Users select items by rotating the wheels to the desired values, which align with a selection indicator. The UIDatePicker class uses a custom subclass of UIPickerView to display dates and times.


1 Answers

I have reported my problem to Apple Technical Support. They confirm the bug :

There does indeed seem to be a difference between the behavior in iOS 6 and 7.

They ask me to report a bug on the bugreport tool. Now, just wait for the fix :)

like image 178
QLag Avatar answered Oct 04 '22 01:10

QLag