UIPickerViewDelegate

Next

Overview

The delegate of a UIPickerView object must adopt this protocol and implement at least some of its methods to provide the picker view with the data it needs to construct itself.

The delegate implements the required methods of this protocol to return height, width, row title, and the view content for the rows in each component. It must also provide the content for each component’s row, either as a string or a view. Typically the delegate implements other optional methods to respond to new selections or deselections of component rows.

See UIPickerView Class Reference for a discussion of components, rows, row content, and row selection.

Tasks


Setting the Dimensions of the Picker View


  • – pickerView:rowHeightForComponent:
  • – pickerView:widthForComponent:

Setting the Content of Component Rows


The methods in this group are marked @optional. However, to use a picker view, you must implement either the pickerView:titleForRow:forComponent: or the pickerView:viewForRow:forComponent:reusingView: method to provide the content of component rows.

  • – pickerView:titleForRow:forComponent:
  • – pickerView:attributedTitleForRow:forComponent:
  • – pickerView:viewForRow:forComponent:reusingView:

Responding to Row Selection


  • – pickerView:didSelectRow:inComponent:

Instance Methods


pickerView:attributedTitleForRow:forComponent:


Called by the picker view when it needs the styled title to use for a given row in a given component.

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component

Parameters

pickerView

An object representing the picker view requesting the data.

row

A zero-indexed number identifying a row of component. Rows are numbered top-to-bottom.

component

A zero-indexed number identifying a component of pickerView. Components are numbered left-to-right.

Return Value

The attributed string to use as the title of the indicated component row.

Discussion

If you implement both this method and the pickerView:titleForRow:forComponent: method, the picker view prefers the use of this method. However, if your implementation of this method returns nil, the picker view falls back to using the string returned by the pickerView:titleForRow:forComponent: method.

Availability

  • Available in iOS 6.0 and later.

Declared In

UIPickerView.h

pickerView:didSelectRow:inComponent:


Called by the picker view when the user selects a row in a component.

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

Parameters

pickerView

An object representing the picker view requesting the data.

row

A zero-indexed number identifying a row of component. Rows are numbered top-to-bottom.

component

A zero-indexed number identifying a component of pickerView. Components are numbered left-to-right.

Discussion

To determine what value the user selected, the delegate uses the row index to access the value at the corresponding position in the array used to construct the component.

Availability

  • Available in iOS 2.0 and later.

Declared In

UIPickerView.h

pickerView:rowHeightForComponent:


Called by the picker view when it needs the row height to use for drawing row content.

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component

Parameters

pickerView

The picker view requesting this information.

component

A zero-indexed number identifying a component of pickerView. Components are numbered left-to-right.

Return Value

A float value indicating the height of the row in points.

Availability

  • Available in iOS 2.0 and later.

Declared In

UIPickerView.h

pickerView:titleForRow:forComponent:


Called by the picker view when it needs the title to use for a given row in a given component.

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

Parameters

pickerView

An object representing the picker view requesting the data.

row

A zero-indexed number identifying a row of component. Rows are numbered top-to-bottom.

component

A zero-indexed number identifying a component of pickerView. Components are numbered left-to-right.

Return Value

The string to use as the title of the indicated component row.

Discussion

If you implement both this method and the pickerView:attributedTitleForRow:forComponent: method, the picker view prefers the pickerView:attributedTitleForRow:forComponent: method. However, if that method returns nil, the picker view falls back to using the string returned by this method.

Availability

  • Available in iOS 2.0 and later.

Declared In

UIPickerView.h

pickerView:viewForRow:forComponent:reusingView:


Called by the picker view when it needs the view to use for a given row in a given component.

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

Parameters

pickerView

An object representing the picker view requesting the data.

row

A zero-indexed number identifying a row of component. Rows are numbered top-to-bottom.

component

A zero-indexed number identifying a component of pickerView. Components are numbered left-to-right.

view

A view object that was previously used for this row, but is now hidden and cached by the picker view.

Return Value

A view object to use as the content of row. The object can be any subclass of UIView, such as UILabel, UIImageView, or even a custom view.

Discussion

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.

Availability

  • Available in iOS 2.0 and later.

Declared In

UIPickerView.h

pickerView:widthForComponent:


Called by the picker view when it needs the row width to use for drawing row content.

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component

Parameters

pickerView

The picker view requesting this information.

component

A zero-indexed number identifying a component of the picker view. Components are numbered left-to-right.

Return Value

A float value indicating the width of the row in points.

Availability

  • Available in iOS 2.0 and later.

Declared In

UIPickerView.h

Next




Copyright © 2012 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2012-09-19



Provide Feedback

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值