Question

I have a UIPickerView, in it's delegate I'm trying to customize the view for a row. I'm using the 3.1 SDK.

So in the delegate I have:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
//    view.backgroundColor = [UIColor redColor];
    return view;
}

From the apple docs: If the previously used view (the view parameter) is adequate, return that. If you return a different view, the previously used view is released. The picker view centers the returned view in the rectangle for row.

When I run this, my UIPickerView control doesn't have any items in it, and after a short while crashes. When I remove this particular method (which is optional for the delegate), I can see the labels I set via the titleForRow method, and it will no longer crash.

I'm pretty new to cocoa (and cocoa-touch), I'm not sure the view.backgroundColor thing will work, but even when returning the unmodified old view (which I must do anyway for most rows) crashes my app.

Am I doing something wrong?

Was it helpful?

Solution

Yes, you implement either –pickerView:titleForRow:forComponent: or –pickerView:viewForRow:forComponent:reusingView:, but not both. What is happening is that it is not calling your –pickerView:titleForRow:forComponent: because it is using your –pickerView:viewForRow:forComponent:reusingView:. You are returning the reusingView: parameter, but that is nil the first time, because there was no "previously used view" for that row.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top