Reusing view in UIPickerView with iOS 7

2020-02-06 18:42发布

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 ...?

1条回答
我只想做你的唯一
2楼-- · 2020-02-06 19:20

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 :)

查看更多
登录 后发表回答