在xib文件中使用UIpickerview

时间:2012-02-22 21:10:55

标签: xcode uitableview xcode4 xcode4.2 uipickerview

我在Xcode 4.2中创建了一个xib文件,我正在寻找将它带到选项卡式应用程序,但我一直在尝试,我对Xcode有点新,编码所以这就是为什么我需要你的帮助我有我的下面的代码,但有一些小错误,但我希望你很多帮助我可能是一个标签应用程序...告诉我无论如何做什么。下面是一个视频供你查看,以防我没有解释它是正确的(这是我的视频,我只针对这个问题http://www.youtube.com/watch?v=e7R5_kGClM0希望视频帮助,但更重要的是你可以帮助我。

有.h但是

中没有错误
#import <UIKit/UIKit.h>

@interface myview : UIViewController <UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource>    


@property (strong, nonatomic) IBOutlet UITableView* tableView;
@property (strong, nonatomic) IBOutlet UIPickerView* pickerView;
@property (strong, nonatomic) NSMutableArray* tableData;
@property (strong, nonatomic) NSMutableArray* pickerData;


@end

这是.m,但我会将其分解

#import "myview.h"

@implementation myview


@synthesize tableView, pickerView, tableData, pickerData;





- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

这是 - (void)viewDidLoad];等等。

- (void)viewDidUnload
{
    [super viewDidUnload];

    tableView.delegate = self;
    tableView.dataSource = self;
    pickerView.delegate = self;
    pickerView.dataSource = self;

    tableData = [[NSMutableArray alloc] init]; // table starts empty
    pickerData = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", nil]; // picker starts with values 1, 2, 3, 4, 5

    [tableView reloadData];
    [pickerView reloadAllComponents];    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (bool)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
    //The number of sections in UITableView
    return 1;

}


- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    // The number of rows in the UITableView
    return [tableData count];

}


- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [ [UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

    }

    // Set the table cell text to the appropriate value in tableDate
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];

    return cell;

}

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Whatever happens when you select a table view row.
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    // The number of sections in the UIPickerView
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    // The number of rows in the UIPickerView
    return [pickerData count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    // The data for each row in the UIPickerView
    return [pickerData objectAtIndex:row];
}

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

    // here I am assuming you want to remove from the picker and add to the table on selection
    [tableData addObject:[pickerData objectAtIndex:row]];
    [pickerData removeObjectAtIndex:row];

    [tableView reloadData];
    [pickerView reloadAllComponents];
}




@end

以下是代码中的错误有两个

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
                          ^^^^^^^^^^^^^^^^^
      **Local declaration of 'tableView hides instance variable**

第二个错误

    [pickerView reloadAllComponents];
            ^^^^^^^^^^^^^^
Local declaration of 'pickerView hides instance variable

非常感谢,我很快就希望你能来到这里!!!

1 个答案:

答案 0 :(得分:1)

如果局部变量和实例变量具有相同的名称,则使用此“tableView隐藏实例变量”。

 - (UITableViewCell*)tableView:(UITableView*)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
            static NSString *cellIdentifier = @"cell";

       UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:cellIdentifier];

            if (cell == nil) {
                cell = [ [UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

      }
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];

    return cell;

}

并且对于pickerView使用此

[self.pickerView reloadAllComponents];