Search code examples
iosobjective-cxcodeuipickerviewuipickerviewdelegate

Why doesn't attributedTitleForRow get called with a UIPickerView?


As per the documentation, picker views prefer to use attributedTitleForRow over titleForRow. If the preferred returns nil, the picker view falls back to titleForRow.

I'm implementing both methods, and attributedTitleForRow never gets called. Even when titleForRow returns nil, and attributedTitleForRow returns an NSAttributedString, it doesn't get called. However, titleForRow always gets called.

Here's what I have:

- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component {
    return @"Hello";
}

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView
             attributedTitleForRow:(NSInteger)row
                      forComponent:(NSInteger)component {
    return [[NSAttributedString alloc] initWithString:@"World"];
}

Solution

  • I didn't see that the viewForRow delegate method was also called. If this method is called, neither titleForRow nor attributedTitleForRow will run.