Ios 设置PickerView中行的标题
我正努力在我的PickerView中设置我的标题。目前,我有一个选择器,可以访问用户的播放列表数据。然后我想把它转移给采摘者。但是,由于didSelectRow方法,我知道选择器数据是正确的。我一直很不走运,试图在PickerView中显示实际数据 **我已将我的代码编辑为现在的代码。目前,“购买”在我的pickerview中反复出现。有办法解决吗Ios 设置PickerView中行的标题,ios,objective-c,xcode,uipickerview,Ios,Objective C,Xcode,Uipickerview,我正努力在我的PickerView中设置我的标题。目前,我有一个选择器,可以访问用户的播放列表数据。然后我想把它转移给采摘者。但是,由于didSelectRow方法,我知道选择器数据是正确的。我一直很不走运,试图在PickerView中显示实际数据 **我已将我的代码编辑为现在的代码。目前,“购买”在我的pickerview中反复出现。有办法解决吗 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInCompone
-(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;
}
问题是您未能将此类设置为picker视图的数据源,并且未能实现数据源方法。因此,没有数据。请参阅。(或者只看一个工作示例,并以此为基础编写代码。)那么您的实际问题是什么?错误的数据显示?没有数据显示?运行时错误?FWIW,关于一个方法中的数据的假设不一定适用于单独的方法。没有数据显示。如果没有数据显示,您的根本原因可能是(除了糟糕的设计——您应该仔细看看Matt关于数据源的回答):(1)titleForRow:没有被调用,(2)pickerView.tag不等于1、2或3(注意,在这种情况下,您没有catchall回退案例),(3)相关数组为零,或(4)objectAtIndex:对于所讨论的数组,它是一个空字符串。在第一个if语句上设置一个断点并逐步执行,每次向前移动时都检查每个变量的值。具体来说,请从
UIPickerViewDataSource
-numberOfComponentsInPickerView:
和“--pickerVie”实现这两个方法w:numberOfRowsInComponent:`。如果要快速测试,只需为两者返回1。那里有数据。我可以使用lblPlaylist.text查看那里有什么数据。但是,数据在选择器中是空白的。因此,我可以选择不同的播放列表。我不确定设置数据源的意思。数据源已连接到播放列表选取器。请证明\u array1
不是nil
。此外,您的代码没有任何意义。您似乎同时为三个不同的选取器视图编码。删除所有代码,仅一个选取器视图的代码除外。