在 xib 文件中使用 UIpickerview

发布于 2025-01-08 13:38:59 字数 4453 浏览 8 评论 0 原文

我已经在 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];etc..

- (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

非常感谢,我希望从你这里来很快 !!!

i have created a xib file in Xcode 4.2 and i am looking at getting it to the tabbed application but i have been trying and I'm a little bit new to Xcode , coding so thats why i need your help i have my code below but there are a few little errors but what i would like you lot to help me with is maybe getting to a tabbed application... well telling me what to do anyhow. heres a video for you to have a look at in case i haven't explained it correct (this is my video i did just for this question http://www.youtube.com/watch?v=e7R5_kGClM0 hope the video help but more importantly you can help me.

There is the .h but the is no errors in this

#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

This is the .m but i will break it up

#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;
}

this is the -(void)viewDidLoad];etc..

- (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

Here Are The Errors In The Code There are Two

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

Second error

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

Thanks Very Much I Hope To Here From You Soon !!!

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

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

发布评论

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

评论(1

一世旳自豪 2025-01-15 13:39:00

使用这个,如果局部变量和实例变量同名,就会出现“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];

Use this, "tableView hides instance variable" occurs if local variable and instance variable have the same name.

 - (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;

}

and for pickerView use this

[self.pickerView reloadAllComponents];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文