1

这是一个非常广泛的问题,而不是针对单个问题的具体问题。如果您不想帮助其中之一,这里是您的警告!

我有一个带有pickerWheel. 在这个选择器上,我想要一份 Cargo 船东的名单。当我按下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]];



}
4

2 回答 2

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;

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

当您运行该对象时,您可以轻松地将对象推送到 adetailView并对其进行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 的情况下编写的,因此它可能根本编译也可能不编译。这也只是一个示例,代码本身可能会也可能不会针对您的特定问题进行优化。

于 2013-01-09T15:07:01.197 回答
1

两个提示:

1.如果你实现了UITableViewDelegate 协议,那么当用户在表格视图中选择一行时你会得到通知:

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

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

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

于 2013-01-09T15:16:08.333 回答