Iphone:分割以逗号分隔的字符串并在pickerview的行中打印每个字符串

发布于 2025-01-07 04:55:58 字数 360 浏览 2 评论 0原文

我得到一个字符串 mild,medium,hot。我用逗号作为分隔符分割字符串。我也需要在 pickerView 中打印它。
我使用以下代码并成功将列表计数设置为 3。

    NSString *spList=[mdict objectForKey:@"spicinesstype"];
    NSArray *list = [spList componentsSeparatedByString:@","];
    NSLog(@"List count:%d",[list count]);
    return [list count];

但是我怎样才能在pickerview中显示所有3个项目

I got a string as mild,medium,hot.I split the string with comma as separator.I need to print it in a pickerView too.
I used the following code and got List count as 3 successfully.

    NSString *spList=[mdict objectForKey:@"spicinesstype"];
    NSArray *list = [spList componentsSeparatedByString:@","];
    NSLog(@"List count:%d",[list count]);
    return [list count];

But how could I display all 3 items in pickerview

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Oo萌小芽oO 2025-01-14 04:55:58

您应该将您的类设置为选择器视图的委托,然后为您的选择器实现这 3 个委托方法

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    return [array count];
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [array objectAtIndex:row];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
    return 1;
}

You should set your class as the delegate of the picker view, Then implement these 3 delegate methods for your picker

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    return [array count];
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [array objectAtIndex:row];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
    return 1;
}
甜尕妞 2025-01-14 04:55:58

首先,您需要通过将 UIPickerViewDelegate 和 UIPickerViewDataSource 放在视图控制器头文件的 @interface 行的末尾来实现它们。

像这样: @interface MyViewController : UIViewController

接下来,您需要将视图控制器设置为选取器视图的委托和数据源。您可以在视图控制器的 - (void)viewDidLoad 方法中添加以下行来执行此操作:

myPickerView.delegate = self;
myPickerView.dataSource = self;

或者,如果您正在使用 Interface Builder,则可以在 Interface Builder 中将其链接起来。

然后您需要在视图控制器源文件中实现这些委托方法。

 (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 1;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    // whatever you want to happen when a row is selected.
}

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

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    return [list objectAtIndex:row];
}

First you need to implement UIPickerViewDelegate and UIPickerViewDataSource by putting them on the end of the @interface line of the header file of your view controller.

Like this: @interface MyViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>

Next you need to set your view controller as the delegate and data source of you picker view. You can either do this in the - (void)viewDidLoad method of your view controller by adding the lines:

myPickerView.delegate = self;
myPickerView.dataSource = self;

Or you can link it up in Interface Builder if you are using that.

Then you need to implement these delegate methods in your View Controller source file.

 (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 1;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    // whatever you want to happen when a row is selected.
}

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

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    return [list objectAtIndex:row];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文