UITableView would not call the delegate - ios

I try to package the UITableView as my own.
The code is:
#interface OPTableView : UIView<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *_dataSource;
id<OPTableviewDelegate>_delegate;
}
#property(retain,nonatomic)id<OPTableviewDelegate>delegate;
#property(retain,nonatomic)NSMutableArray *dataSource;
.m:
#implementation OPTableView
#synthesize delegate=_delegate;
#synthesize dataSource=_dataSource;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
tableView=[[UITableView alloc] initWithFrame:frame];
tableView.delegate=self;
tableView.dataSource=self;
_dataSource=[NSMutableArray array];
[self addSubview:tableView];
[tableView release];
}
return self;
}
#pragma table delegate
- (void)tableView:(UITableView *)tab didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
When I create OPTableView,it can show in the screen.But the problem is that when I did select one table,it would not call the delegate method :didSelectRowAtIndexPath,but what is worse,it breaks down.

Try to set your cell's selection style to some non-none value. Also note you should implement all the required delegate messages of your table - did you do that?

What is OPTableViewDelegate? Your table's delegate is of type OPTableViewDelegate, not UITableViewDelegate.

all,I have made a really stupid mistake.
In the 'initWithFrame' method,the code:_dataSource=[NSMutableArray array]; is absolutely wrong.
I delete this code"_dataSource=[NSMutableArray array]; ",everything works.
Thanks all.

Related

Custom UITableViewCell without dequeueReusableCellWithIdentifier

So I have a custom UITableViewCell:
TestTableViewCell.h
#import <UIKit/UIKit.h>
#interface TestTableViewCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *testCellLabel;
#end
TestTabelViewCell.m
#import "TestTableViewCell.h"
#implementation TestTableViewCell
-(id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
_testCellLabel = [[UILabel alloc] init];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
And then I have view controller with a table view that uses the custom table view cell. However this issue is that I don't want to use dequeueReusableCellWithIdentifier within the cellForRowAtIndexPath. I instead want to have an array of cells.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#end
ViewController.m
#import "ViewController.h"
#import "TestTableViewCell.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#property (strong, nonatomic) NSArray *myTableViewCells;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSArray *)myTableViewCells {
TestTableViewCell *cell1 = [[TestTableViewCell alloc] init];
cell1.testCellLabel.text = #"one";
cell1.backgroundColor = [UIColor blueColor];
TestTableViewCell *cell2 = [[TestTableViewCell alloc] init];
cell2.testCellLabel.text = #"two";
cell1.backgroundColor = [UIColor greenColor];
if (!_myTableViewCells) {
_myTableViewCells = #[cell1, cell2];
}
return _myTableViewCells;
}
#pragma mark - UITableView delegate functions
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.myTableViewCells.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TestTableViewCell *cell = self.myTableViewCells[indexPath.row];
return cell;
}
#end
The problem is that there is no testCellLabel appearing in the table view cell. I know the cells are there, because I set their background colour.
After talking to a few people, apparently I need to do some sort of loading from the XIB or the NIB for the UI to load properly? Even though the label is defined in the cell in the storyboard.
I know this is going against the norm and that Apple really wants you to use dequeueReusableCellWithIdentifier, but I know it won't work in the situation I need it in. I have done the reading on that much so please don't just tell me to use it. This code example is just very basic for example sake and ease of use.
Any help would be greatly appreciated.
TestTableViewCell *cell1 = [[TestTableViewCell alloc] init];
Creates a new TestTableViewCell object and does not instantiate it from the storyboard like you're thinking it does. Therefor all outlets created will be nil and simply not show up. The fact that you can set the background colour is not evidence that your implementation works.
You need to use dequeueReusableCellWithIdentifier. You say that it doesn't work for your problem.. show me how it doesn't work and I will tell you why you're wrong.
Edit
I see in your comments you say your cell needs a custom setter. Well, when you use dequeueReusableCellWithIdentifier you can do all setup work in awakeFromNib (If using a xib file) OR initWithCoder if you are using the storyboard.
You can create cell without dequeueResableCellWithIdentifer.
[[UITableViewCell alloc]initWithStyle:<#UITableCellStyle#> resueIdentifier:<#(nullable *NSString)#>]

reload data in UITABLEVIEW object c when clicking button

I try to search this problem in this site and I found this link How to insert items to a UITableView when a UIButton is clicked in iOS. But my problem is, I already copy the code on that link and It doesn't reload the data when I insert a value to my array.
here's the code "ViewController.m"
#import "ViewController.h"
#interface ViewController ()
#property(nonatomic,strong) NSMutableArray * array;
#property(nonatomic,weak) IBOutlet UITableView * tableView;
#end
#implementation ViewController
-(NSMutableArray *) array{
if(_array==nil){
_array=[[NSMutableArray alloc] init];
}
return _array;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)addInfo:(UIBarButtonItem *)sender {
[self.array addObject:#"sample"];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"TodoListItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [self.array objectAtIndex:indexPath.row];
return cell;
}
#end
here's the code "ViewController.h"
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
#end
Is there something wrong with my code or there is something that I need to setup to my tableview? I'm very confuse of this and try to figure out the missing part of my code. I'm still studying the code of object c and I'm still noob for this. Please help me and thanks in advance.
How did you insert the UITableView?, on nib or programatically?, either way you need to flag the table's delegate and datasource, if on nib, right click on table and see that delegate and data source are connected to the files owner, if programatically check
self.MyTable.datasource = self
self.MyTable.delegate = self
please see how to check if connected [dataSource and delegate should be with a dot, if not click on circle and drag line to file's owner for both]
table delegate and datasource on interface builder "NIB"
Also please note that on this image outlet is not connected, you have to connected also to call the reload as you are doing now
edit, check if the button is connected?, put a log or a break point on ibaction for your button to know if is called
edit 2, you have to init the array, are you calling it? do this in view will appear or when you want to use it
edit 3, try this
- (IBAction)addInfo:(UIBarButtonItem *)sender {
if(self.array==nil){
self.array=[NSMutableArray array];
}
[self.array addObject:#"sample"];
[self.tableView reloadData];
}

How to create a Common Custom TableView Class?

I want to create a custom tableView class, Which can be used in any view Controller. I have to just create tableview object and set an array and frame of tableview . Then this tableview will be add as subview on my view. and also give me a click event.
I just want to avoid writing tableview datasource and delegate method in every viewController class.
Take a viewController or tableviewController class and code all the delegates and data source methods there. now in you view controller where you want to make it as a subview call the tableview class and add it as a subview.
EX:
TableviewContrller *libaray =[TableviewContrller new];
[libaray willMoveToParentViewController:self];
[self.view addSubview:libaray.view];
[self addChildViewController:libaray];
To hide write this code in your tableview controller class
[self.view removeFromSuperView];
As you are using a reusable class you need to send the array information to that class. along with it it will be better to send either class name or setting tag value to tableview
So in your tableview class write this
-(id)initWithInformationArray :(NSMutableArray *)dataArray andTagValueforTableview :(int) tagValue
{
self = [super init];
if (self != nil)
{
NSLog(#"%#", dataArray);
}
return self;
}
Now sub viewing will be like this
TableviewContrller *libaray =[[TableviewContrller alloc]initWithInformationArray:YOURARRAY andTagValueforTableview:TAGVALUE];
[libaray willMoveToParentViewController:self];
[self.view addSubview:libaray.view];
[self addChildViewController:libaray];
Hope this will help.
May be you can use UITableViewController.
UITableViewController is a subclass of UIViewController, when you create a subclass of UITableViewController, the template has the usual methods of tableview datasource and delegate methods.
You'll need to create a custom class and create your own delegate in that class for UITableView. Now whenever you create a UITableView assign that custom class as the class for UITableView.
If you don't know how to create custom delegates then check below links:
http://www.alexefish.com/post/522641eb31fa2a0015000002
http://ios-blog.co.uk/tutorials/quick-tip-create-your-own-objective-c-delegate-protocol/
Hope this will help you :)
You can create BaseTableView class.
#interface BaseTableView : UITableView <UITableViewDelegate, UITableViewDataSource>
{
NSArray* listObject;
}
#property (nonatomic, retain) NSArray *listObject;
-(id) initWithFrame:(CGRect)frame style:(UITableViewStyle)style;
#end
#implementation BaseTable
#synthesize listObject;
-(id) initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
if(self = [super initWithFrame:frame style:style])
{
self.dataSource = self;
self.delegate = self;
}
return self;
}
-(void)setListObject:(NSArray *)listObjectRef
{
[listObject release];
listObject = [listObjectRef retain];
[self reloadData];
}
-(void) dealloc
{
[listObject release];
[super dealloc];
}
#end
Inherit this class for specific use and override following methods according to needs
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
In your ViewController class use following code
SpecificTableView *table = [[SpecificTableView alloc] init];
[table setListObject:((FRFTReportList*)obj)];
Hopefully this will help.

Save selected row in Table View and get back to main screen UIButtons [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Plz code Help Can anyone tell me how to do that task?
In main screen user selects footballer,in 2nd screen in Table view cell user select specific row and save that row and go back to main view.in main view then it shows the specific row videos.
Basically i want to know about speific row selection,save that selection in table view and show thier contetnts in main screen.
go through the below code, it implements the delegate concept and also implements the solution for ur question hope this helps u :)
//in your main view controller
#import "ViewController.h"
#import "FootBallPlayersViewController.h"
#interface ViewController ()<FootballPlayerDelegate>//confirms to this delegate
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)whenSelectButtonClicked:(id)sender
{
FootBallPlayersViewController *controller = [[FootBallPlayersViewController alloc]initWithNibName:#"FootBallPlayersViewController" bundle:nil];
controller.delegate = self; //u must set to self
[self presentViewController:controller animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)selectedFootBallPlayer:(NSString *)player
{
//implementation of your delegate method
//hear u are getting the football player name and u can continue further hear
NSLog(#"%#",player);
if([player isEqualToString:#"player1"])
{
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTitle:player forState:UIControlStateNormal];
[aButton addTarget:self action:#selector(whenFirstPlayerButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; //add the target to self for click events
aButton.frame = CGRectMake(50, 50, 200, 55);
[self.view addSubview:aButton];
}
else
{
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTitle:player forState:UIControlStateNormal];
aButton.frame = CGRectMake(50, 105, 200, 55);
[aButton addTarget:self action:#selector(whenSecondPlayerButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; //same hear
[self.view addSubview:aButton];
}
}
//now define the action methods
- (void)whenFirstPlayerButtonClicked:(UIButton *)sender
{
NSLog(#"player 1 video start");
}
- (void)whenSecondPlayerButtonClicked:(UIButton *)sender
{
NSLog(#"player 2 video start ");
}
#end
in the view that contain's the tableview do somthing like this
//in FootBallPlayersViewController.h
#import <UIKit/UIKit.h>
#protocol FootballPlayerDelegate <NSObject> //define a protocol named FootballPlayerDelegate
- (void)selectedFootBallPlayer:(NSString *)player;
#end
#interface FootBallPlayersViewController : UIViewController
{
NSArray *players;
NSString *selectedPlayer;
}
#property (retain, nonatomic) IBOutlet UITableView *playerTable;
#property (nonatomic, assign) id<FootballPlayerDelegate>delegate; //create a delegate
#end
in your FootBallPlayersViewController.m file
#import "FootBallPlayersViewController.h"
#interface FootBallPlayersViewController ()<UITableViewDataSource,UITableViewDelegate>
{
}
#end
#implementation FootBallPlayersViewController
#synthesize delegate; //synthesizing the delegate
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
players = [[NSArray alloc]initWithObjects:#"player1",#"player2", nil];
// players = [[NSArray alloc]initWithObjects:#"player1","player2", nil];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
[players release];
[_playerTable release];
[super dealloc];
}
- (IBAction)whenDoneButtonClicked:(id)sender {
//when done button clicked -->
//send a delegate to main controller
if([self.delegate respondsToSelector:#selector(selectedFootBallPlayer:)])//to avoid crash
{
[self.delegate selectedFootBallPlayer:selectedPlayer]; //call the delegate method hear
}
//dismiss the view
[self dismissViewControllerAnimated:YES completion:nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return players.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:#"cell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.textLabel.text = [players objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//u can manage check mark and all, i am getting the selected player name
selectedPlayer = [players objectAtIndex:indexPath.row];
}
#end
Simple solution ...
As you are a newbie , I am clarifying each point.
First make a property in AppDelegate.h
#property int selectedRow;
Save the selected indexpath.row in 2nd screen that is your Table view screen, and also import AppDelegate.h
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.appDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
self.appDelegate.selectedRow=indexPath.row; //saving the row
}
On main screen's viewWillAppear()
-(void)viewWillAppear:(BOOL)animated
{
if(self.appDelegate.selectedRow!=-1)//check wether row is selected or not
{
//action to show the specific row videos
}
}
Good Ways to accomplish this:
Custom Delegate
NSNotificationCenter
NSUserDefaults (edit: unnecessary disk writes)
Maintaining a Common NSObject Subclass and refreshing data on -willAppear
Other Ways:
Database (Core Data / SQLite) or plist (all too heavy for your case) (edit: unnecessary disk writes)
UIPasteBoard
A quick delegate tutorial:
Part 1: Creating the delegate
Suppose this is in the .h of the UITableViewController subclass that I have named YourTableViewControllerClassName
//declare the protocol
#class YourTableViewControllerClassName;
#protocol YourTableViewControllerClassNameDelegate <NSObject>
//#required //uncomment to specify required delegate methods as below
//- (void)requiredMethodNotUsedForThisExample;
#optional
- (void)selectedRow: (NSString *)selectedObj;
#end
#interface YourTableViewControllerClassName : UITableViewController
//declare a weak property to store any object
#property (nonatomic, weak) id <YourTableViewControllerClassNameDelegate> delegate;
#end
Suppose this is the -didSelectRowAtIndexPath of the corresponding UITableViewController subclass:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//the following line is the main thing and can be called
//in any method within this class (placed wisely)
if([[self delegate] respondsToSelector:#selector(selectedRow)]) { //avoid crash
[[self delegate] selectedRow:cell.textLabel.text];
}
[self.navigationController popViewControllerAnimated:YES];
}
Part 2: Implementing the delegate
Suppose this is the code somewhere in the former UIViewController subclass:
//call this method somewhere
-(void)pushMyTableViewController
{
//declare "UILabel lblText;" in the .h of this class
//lblText = [UILabel alloc] init];
//[lblText setFrame: CGRectMake(0,0,100,35)];
//[self.view addSubview:lblText];
YourTableViewControllerClassName *tvcObj = [[YourTableViewControllerClassName alloc] init];
//for the following line, remember to declare
//<YourTableViewControllerClassNameDelegate> in the .h of this class
//hence declaring that this class conforms to the delegate protocol
[tvcObj setDelegate:self];
[self.navigationController pushViewController:tvcObj animated:YES];
}
And this will be the delegate method you could implement in the former UIViewController subclass:
#pragma mark - Optional YourTableViewControllerClassName Delegate Methods
-(void)selectedRow:(NSString *)selectedObj
{
[lblText setText:selectedObj];
}
NOTE: This will not solve your particular issue because we are only setting a label depending on the selected row from the UITableViewController subclass.
The point was to show how delegation works.
Also, if you can get the cell.textLabel.text and set it on a UILabel in the former class then you can make changes at the appropriate places (mainly the method/s within #protocol)and pass the array index of the selected item instead or any object/variable/whatever that makes your life easier
*If you want something easier then go for NSNotificationCenter or NSUserDefaults or maybe even UIPasteBoard (if it floats your boat)
Use the tableView delegate called when you select any row
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
appDelegate.selectedindex = indexpath.row;
or
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:indexpath.row] forKey:#"SelcetedIndex"];
}
then there is 3 things you can do to get your selected index
1) make a app delegate variable for index path so that you can set here and get the value on other controller
// add property at appDelegate file
#property int selectedIndex;
2) Using NSUserDefault to set the selected index value
// read userDefault value
[[[NSUserDefaults standardUserDefaults] objectForKey:#"SelcetedIndex"] intValue];
3) using delegate to return back the value to previous controller
// try to google and first understand the concept and let me know if you want to go with delgate

Choose objects inside UITableView chosen by UIPickerView

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

Resources