views:

223

answers:

1

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?

+1  A: 

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.

newacct
thanks, I'll have to try setting a view, I just assumed that the reusingView parameter would be the default view.I'll try it in ~10 hrs
Prody
that was it, thanks
Prody