Sarah
Sarah

Reputation: 1595

Custom UIPickerView

I want to use custom picker view that will add a check sign in front of selected row. I have used apple sample code for custom UIPickerView on the UICatalog example. I was able to add the check sign for a given row when creating the picker. But I failed to add it when the user rotate the wheel to select new row and also to remove it from the previously added row. Any help would be appreciated. Thanks,

Upvotes: 1

Views: 3451

Answers (2)

Till
Till

Reputation: 27587

As you are not providing any code, all we can give you is a general advise;

  • make sure the viewController showing your UIPickerView instance is inheritating the UIPickerViewDelegate-protocol
  • set the delegate of your UIPickerView towards this viewController; e.g. pickerView.delegate = self;
  • implement - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
  • within the above implementation, remove any checkmark previously added and add a new one to the row selected.

Upvotes: 0

DimGun
DimGun

Reputation: 111

1) Create subclass of UIView that will represent row in picker. Define property, for example, isChecked, which will show/hide checkmark on this view

2) In the – pickerView:didSelectRow:inComponent: call – viewForRow:forComponent: for previously selected row, set isChecked=NO

3) call – viewForRow:forComponent: for currently selected row and set isChecked=YES;

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{ 
MyCustomRowView *prevRowView = [pickerView viewForRow:currentlySelectedRow forComponent:component];
prevRowView.isChecked = NO;

MyCustomRowView *currentRowView = [pickerView viewForRow:row forComponent:component];
currentRowView.isChecked = YES;
//then save currently selected row
currentlySelectedRow = row;
}

4) you also should check currently selected row when you is requested for it:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
     ....
     //Create or reuse view
     ....
     rowView.isChecked = (row == currentlySelectedRow);
}

Upvotes: 3

Related Questions