UIPickerView只显示一个组件;应该显示两个

时间:2011-07-02 23:46:07

标签: iphone objective-c cocoa-touch uipickerview

有一个UIPicker,它应该显示两个组件(选择器视图中的列),但只显示一个。

找不到错误;它构建正确。第二个组件是空的;没有数据显示。

InstaEmailViewController.h

#import <UIKit/UIKit.h>

@interface InstaEmailViewController : UIViewController
<UIPickerViewDataSource, UIPickerViewDelegate> {
    NSArray* activities_ ;
    NSArray* feelings_ ;
}

InstaEmailViewController.m

#import "InstaEmailViewController.h"

@implementation InstaEmailViewController

- (void)dealloc
{
    [activities_ release];
    [feelings_ release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a    nib.
- (void)viewDidLoad
{
    activities_ = [[NSArray alloc] initWithObjects:@"sleeping", @"working", @"thinking",       @"crying", @"begging", @"leaving", @"shopping", @"hello worlding", nil];

    feelings_ = [[NSArray alloc] initWithObjects: @"awesome", @"sad", @"ambivalent",    @"nauseous",@"psyched", @"confused", @"hopeful", @"anxious", nil];

    [super viewDidLoad];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row     forComponent:(NSInteger)component {
    if (component == 0) {
        return [activities_ objectAtIndex:row];
    } else {
        [feelings_ objectAtIndex:row];
    }

    return nil;
}

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:    (NSInteger)component {
    if (component ==0) {
        return [activities_ count];
    } else {
        return [feelings_ count];
    }
}

@end

1 个答案:

答案 0 :(得分:3)

您没有在pickerView:titleForRow:inComponent:方法中为第二个组件返回值。

if (component == 0) {
    return [activities_ objectAtIndex:row];
} else {
    [feelings_ objectAtIndex:row];
}

你应该做

return [feelings_ objectAtIndex:row];