选择UIPickerView选择的UITableView中的对象

时间:2013-01-09 14:56:40

标签: ios objective-c xcode uitableview uipickerview

这是一个非常广泛的问题,而不是关于单个问题的具体问题。如果你不想帮助其中一个,这是你的警告!

我的查看带有 pickerWheel 。在这个选择器上,我想要一份货运船主名单。当我按下OK按钮时,我想要一个UITableView由这个拥有者拥有的船只填充。不多,约5艘船。我希望tableView与选择器位于同一视图上。

当我在列表中按下一艘船时,我希望被引导到另一个视图,我可以在另一个视图中添加可变数字,例如运输载荷等。

我知道如何制作和填充并从提取器中提取数据,我不知道如何使用这些数据以某种方式填充表格,我不知道如何使用表格上的对象将我链接到查看我可以根据发送者的不同来设置不同的值。

我在编程方面表现不佳,所以带代码的全面解决方案会很棒!:)

编辑:我到目前为止的代码

.h file
#import <UIKit/UIKit.h>
#import "StartupViewController.h"

@interface CargoViewController : UIViewController

@property(strong,nonatomic) IBOutlet UILabel *testLabel; //label I use to test stuff instead of LOG
@property(strong,nonatomic) IBOutlet UIButton *findOwner; //button to summon pickerWheel
@property(strong,nonatomic) IBOutlet UIButton *findShip; //Button to confirm selection in picker and unhide tableView
@property(strong,nonatomic) NSArray *boatOwners; //Array for boat owners.
@property(strong,nonatomic) NSMutableArray *boatsForOwner; //My idea was that when you select a owner, i have a if-else series, so say i select "Bob", I code to add his boats to the array. Does this work? IDK
@property(strong,nonatomic) IBOutlet UITableView *boatsTableView; //Table view
-(IBAction)findOwnerButtonPressed:(id)sender;
-(IBAction)findShipButtonPressed:(id)sender;
@property(strong, nonatomic) IBOutlet UIPickerView *shipOwners; //ship owner picker wheel

@end

.m文件:

 #import "CargoViewController.h"

@interface CargoViewController ()


@end

@implementation CargoViewController
@synthesize testLabel, findOwner, findShip,shipOwners, boatsTableView;
@synthesize boatsForOwner, boatOwners;


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

- (void)viewDidLoad
{
[self getDataFromDensity ];

boatOwners = @[@"owner1", @"owner2", @"owner3"]; 

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) getDataFromDensity
{
NSString *getData = [[NSUserDefaults standardUserDefaults]
                     objectForKey:@"globalMathString"]; //Gets a number from another view, wanted to use in the information about boat view later on. 
testLabel.text = getData;
[boatsForOwner setValue:@"5" forKey:getData];
}
-(IBAction)findOwnerButtonPressed:(id)sender
{
findShip.hidden = NO;
shipOwners.hidden = NO;

boatsTableView.hidden = YES;
}
-(IBAction)findShipButtonPressed:(id)sender
{
shipOwners.hidden = YES;
findShip.hidden = YES;
boatsTableView.hidden = NO;

}
#pragma mark -
#pragma mark PickerView DataSource

- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [boatOwners count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView
         titleForRow:(NSInteger)row
        forComponent:(NSInteger)component
{
return [boatOwners objectAtIndex:row];
}


#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
  inComponent:(NSInteger)component
{
NSString *boatsFromOwner = [boatOwners objectAtIndex:[shipOwners selectedRowInComponent:0]];



}

2 个答案:

答案 0 :(得分:1)

在伪方面,我认为你需要这样的东西。

cargoshipOwner.h // Class that holds all details for a cargoship owner.

该类可能具有属性

NSMutableArray *listOfCurrentlyOwnedShips;

其中包含:

cargoShip.h // CargoShip class.

NSInteger carriageLoad;
UIColor *shipColor;
NSString *model;
NSString *name;

听起来您希望使用NSMutableArray填充cargoshipOwners填充您的选择器视图,当选择一个时,您希望通过 对象的属性listOfCurrentlyOwnedShips并填充UITableView

当你开始运行时,可以轻松地使用detailView对象推送到cargoship并进行操作。

一旦您设置了UITableViewDelegate并且它的功能正常,您应该在.m文件中具有以下功能。

您需要使用函数cellForRowAtIndexPath:绘制您的单元格,示例可能如下所示:

-(UItableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexpath
{

    // Create a cell with 300 width and 80 height.
    UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectmake(0,0,300,80)]; 

    // Create a label.
    UILabel *lblCargoShipOwner = [[UILabel alloc] initWithFrame:CGRectMake(10,12,200,20)];
    lblCargoShipOwner.tag = 1;
    lblCargoShipOwner.textColor = [UIColor blackColor];
    lblCargoShipOwner.backGroundColor = [UIColor clearColor];    


    // Add it to your cell.
    [cell.contentView addSubView:lblCargoShipOwner];


    // Get the label.
    UILabel *textLabel = (UILabel *)[cell viewWithtag:1];

    // Add the owner's name to the label.
    textLabel.text = [[listOfCargoShipOwners objectAtIndex:indexPath.row]sName];

    // important to note is that listOfCargoShipOwners is your datasource.
}

当您填充UITableView后,您可以点按一个单元格并指定tableView:didSelectRowAtIndexPath:内的内容。示例:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexpath *)indexPath
{

    // Push the owner's list of ships to a UITableViewController

    DetailViewController *dvc = [[DetailViewController alloc] initWithStyle:UITableViewStylePlain andCargoShipList: [[listOfCargoShipOwners objectAtindex:indexPath.row]listOfCurrentlyOwnedShips]];



}

免责声明:这是在没有任何形式的IDE的情况下编写的,因此可能会编译也可能根本不编译。这也只是一个示例,代码本身可能会或可能不会针对您的特定问题进行优化。

答案 1 :(得分:1)

两个提示:

1。如果您实施UITableViewDelegate protocol,那么当用户在表格视图中选择一行时,您会收到通知:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

因此,您可以相应地更改视图的内容,然后在UINavigationController中推送该视图。

2. 检查UITableViewDataSource protocol以了解如何将内容放入表格视图中。