在PickerView中设置Row的标题

时间:2015-06-17 02:46:58

标签: ios objective-c xcode uipickerview

我正在努力尝试在我的PickerView中设置我的titleForRow。目前,我有一个访问用户播放列表数据的选择器。然后我想将它转移到选择器。但是,由于didSelectRow方法,我知道选择器数据是正确的。我试图在PickerView中显示实际数据是不吉利的。

**我已将我的代码编辑为现在的代码。目前,在我的挑选者视图中反复重复购买。有办法解决吗?

   -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [_roundArray count];
    return [_timeArray count];

    MPMediaQuery *myPlaylistsQuery = [MPMediaQuery playlistsQuery];
    NSArray *playlists = [myPlaylistsQuery collections];

    return [playlists count];

}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView.tag == 1) {
        lblRound.text = [_roundArray objectAtIndex:row];
    }
    else if (pickerView.tag == 2) {
        lblTime.text = [_timeArray objectAtIndex:row];
    }
    else if (pickerView.tag ==3) {

        MPMediaQuery *myPlaylistsQuery = [MPMediaQuery playlistsQuery];
        NSArray *playlists = [myPlaylistsQuery collections];

        for (MPMediaPlaylist *playlist in playlists) {
            lblPlaylist.text = [playlist valueForProperty: MPMediaPlaylistPropertyName];
    }
}

}

#pragma mark Picker Delegate Methods

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

    NSString *title;

    if (pickerView.tag == 1) // this is otherPickerview
    {
        title=[_roundArray objectAtIndex:row]; // your logic to get title for otherpickerview

    }
    else if (pickerView.tag == 2) // this is citysPickerview
    {
        title=[_timeArray objectAtIndex:row]; // your logic to get title for cityspickerview

    }
    else if (pickerView.tag == 3)
    {
        [self showPlaylistData];

        MPMediaQuery *myPlaylistsQuery = [MPMediaQuery playlistsQuery];
        NSArray *playlists = [myPlaylistsQuery collections];

        for (MPMediaPlaylist *playlist in playlists)
            title = [playlist valueForProperty: MPMediaPlaylistPropertyName];
    }

    return title;

}

1 个答案:

答案 0 :(得分:2)

问题是您未能将此类设置为选择器视图的数据源,并且您未能实现数据源方法。因此,没有数据。咨询documentation on UIPickerViewDataSource。 (或者只是查看任何有用的示例并将代码基于此。)