ios - 选择 UIPickerView 选择的 UITableView 中的对象

标签 ios objective-c xcode uitableview uipickerview

这是一个非常广泛的问题,而不是关于单个问题的具体问题。如果您不想帮助其中一项,请注意以下事项!

我有一个带有 pickerWheel View 。在这个选择器上,我想要一份货船船东名单。当我按下 OK 按钮时,我希望 UITableView 由该所有者拥有的船只填充。不多,大约5艘。我希望 tableView 与选择器位于同一 View 中。

当我按下列表中的一艘船时,我想被引导到另一个 View ,我可以在其中添加来自另一个 View 的可变数字,例如运输负载等。

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

我不擅长编程,所以完整的代码解决方案会很棒!:)

编辑:我目前拥有的代码

.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]];



}

最佳答案

在伪思维中,我认为您需要这样的东西。

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;

听起来你想用一个填充了 cargoshipOwnersNSMutableArray 来填充你的 pickerview,当你选择一个时你想传递 对象的属性 listOfCurrentlyOwnedShips 并填充 UITableView

当你让它运行时,你可以轻松地将 cargoship 对象推送到 detailView 并对其进行操作。

一旦你设置了你的 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 的情况下编写的,因此它可能会或可能根本不会编译。这也只是一个示例,代码本身可能会或可能不会针对您的特定问题进行优化。

关于ios - 选择 UIPickerView 选择的 UITableView 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14237988/

相关文章:

ios - 以多种方式通过 RACSignal 设置按钮的启用属性?

objective-c - NSImage 到 NSBitmapImageRep

iphone - The Amazing Audio Engine AERecorder 不录音

ios - Xcode 仪器

ios - 我如何从 HKHealthStore 中删除多个对象?

ios - 在 iPhone App 中使用 AVSystemController

ios - 应用程序关闭后按钮保持隐藏状态

swift - 使用 Firebase 和 Swift 从未知的 child 名字中检索数据

ios - 无法在 Swift 应用中导入 Firebase

android - 使用C++作为后端的跨平台移动开发