1

This is a very broad question, not a specific question about a single problem. If you don't want to help with one of these, here is your warning!

I have a View with a pickerWheel. On this picker, I want to have a list of Cargo ship owners. When I press an OK Button, I want a UITableView to be populated by the boats this owner has. Not many, about 5 ships. I want the tableView to be on the same view as the picker.

When I press a ship in the list, I want to be directed into another view where I can add variable numbers from another view, like carriage load etc.

I know how to make and populate and extract data from the picker, and I DO NOT KNOW how to use this data to somehow populate a table and i do not know how to use a object on a table to link me to a view where i can set different values depending on who sent it.

I am bad at programming, so fullout solutions with code would be great!:)

EDIT: Code i have so far

.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 file:

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



}
Oscar Apeland
  • 6,422
  • 7
  • 44
  • 92

2 Answers2

1

In pseudothinking i think you'll need something like this.

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

The class probably has a property

NSMutableArray *listOfCurrentlyOwnedShips;

Which is filled with:

cargoShip.h // CargoShip class.

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

It sounds like you want to populate your pickerview with a NSMutableArray filled with cargoshipOwners and when one is selected you want to pass the object's property listOfCurrentlyOwnedShips and populate the UITableView

When you've got that running you can easily push to a detailView with the cargoship object and manipulate it.

Once you have set up your UITableViewDelegate and it's functions properly you should have the following functions in your .m file.

You'll need to use the function cellForRowAtIndexPath: to draw your cells, an example could look like this:

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

When you've populated your UITableView you can tap a cell and specify what happens within tableView:didSelectRowAtIndexPath: An example:

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



}

DISCLAIMER: This was written without any form of IDE so it may or may not compile at all. This is also just an example, the code itself may or may not be optimized for your specific problem.

Jens Bergvall
  • 1,617
  • 2
  • 24
  • 54
  • This seems like about the same stuff I had imagined. My biggest problem is still figuring out how to populate a list in a view, not a fullscreen list, with different content. Almost all tuts i find on the subject are pretty far away from what I need. Plus my lack of knowledge on how to use the table and just general obj-c – Oscar Apeland Jan 09 '13 at 15:14
  • I'll expand my post with more code later, going into more detail how you populate and push objects. – Jens Bergvall Jan 09 '13 at 15:31
  • Do you want any of my already existing code? Not much but still – Oscar Apeland Jan 10 '13 at 08:42
  • Add any code you think will be of any relevance to your original post. – Jens Bergvall Jan 10 '13 at 09:07
1

Two hints:

1. If you implement the UITableViewDelegate protocol, then you will be notified when the user selects a row in the table view:

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

So you can change the contents of the view accordingly, and then push that view in your UINavigationController.

2. Check the UITableViewDataSource protocol to know how to put the contents inside the table view.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187