Create a two tableview list using one TableViewController class - iphone

I want to do an sample application where in a view i have two buttons one is COUNTRY and another is STATE.
when i click on a country button the country list should appear in a tableview class like a popup and when i click on state button state list should appear in a tableview like popup,So how can i do this please suggest with sample code.
NOTE:I should use only one TableViewcontroller class for both country and state Data list.

Here is the Code:
RootViewController.h
#interface RootViewController : UIViewController {
UIButton *btnCountry;
UIButton *btnState;
NSMutableArray *tempArray;
NSMutableArray *countryArray;
NSMutableArray *stateArray;
IBOutlet UITableView *tempTable;
}
#property (nonatomic,retain) UIButton *btnCountry;
#property (nonatomic,retain) UIButton *btnState;
#property (nonatomic,retain) NSMutableArray *countryArray;
#property (nonatomic,retain) NSMutableArray *stateArray;
#property (nonatomic,retain) NSMutableArray *tempArray;
#property (nonatomic,retain) UITableView *tempTable;
- (IBAction) showState:(id)sender;
- (IBAction) showCountry:(id)sender;
#end
RootViewController.m
#implementation RootViewController
#synthesize btnState,btnCountry, stateArray,countryArray,tempArray;
#synthesize tempTable;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
tempTable.hidden = YES;
countryArray = [[NSMutableArray alloc]initWithObjects:#"India",#"Pakistan",#"USA",nil];
stateArray = [[NSMutableArray alloc]initWithObjects:#"Gujarat",#"Maharashtra", #"Karnataka",nil];
tempArray = [[NSMutableArray alloc]init];
}
- (IBAction) showCountry:(id)sender
{
btnCountry = (UIButton *)sender;
tempArray = countryArray;
[tempTable reloadData];
if([btnCountry isSelected])
{
tempTable.hidden = YES;
btnCountry.selected = NO;
}
else
{
tempTable.hidden = NO;
btnCountry.selected = YES;
}
}
- (IBAction) showState:(id)sender
{
btnState = (UIButton *)sender;
tempArray = stateArray;
[tempTable reloadData];
if([btnState isSelected])
{
tempTable.hidden = YES;
btnState.selected = NO;
}
else
{
tempTable.hidden = NO;
btnState.selected = YES;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tempArray count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [tempArray objectAtIndex:indexPath.row];
return cell;
}
#end

The question is not clear enough ,, what did you try? ..
But, You can use UIPopOverController for that
see
THIS LINK
Or Just a Static UiTableView in the same Nib file ,, make it hidden when you don't need it .

You can use One Tableview for this: Here i am attaching only the logic.
In viewdidload,
There will be two arrays countryArray and stateArray.
There will be a third array : tempArray
there are two buttons: button1 and button2
tableview.hidden=YES;
in button1Action assign the countryArray to tempArray and also [tableview reload]
in button2Action assign the stateArray to tempArray and also [tableview reload]
then in tableview delegates,
(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return [tempArray count];
}
and then in
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
use tempArray.......
}
try this...and if you want more detailed pls inform....

Take Two tableViews and two Buttons.
Place each tableview below each of the button.
Initially both the TableViews are hidden. When the Button is clicked show the TableView with Animation.

I think you should only use one TableViewController but with different data sources.

Related

Passing Data from Dynamic tableView to Static TableView

Hi Let me try to clarify my issue. I have two TableViews, one is static and the other is dynamic. The static= RootVC and Dynamic=FirstVC. In FirstVC i have data that I want to select,save and pass the saved data to a UILabel in RootVC. 1)When I run my App data is selected however it is not saved or passed to my rooVC. I was using delegates and was advice not to use "delegate" but use "Blocks". But still i'm facing the same issue. Here is my code:
in rootVC.h
#import <UIKit/UIKit.h>
#interface RootViewController : UITableViewController
{
NSString *getRepeatLabel;
}
#property (strong, nonatomic) IBOutlet UILabel *repeatLabel;
#property (strong, nonatomic) IBOutlet UILabel *repeatDetail;
#property (nonatomic,strong) NSString *getRepeatLabel;
#end
in my rootVC.m
#import "RootViewController.h"
#interface RootViewController ()
#end
#implementation RootViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_repeatLabel.text = #"Repeat";
_repeatDetail.text = getRepeatLabel;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIViewController *destinationController = segue.destinationViewController;
if( [destinationController isKindOfClass:[FirstViewController class]] )
{
[(FirstViewController *)destinationController setCompletionBlock:^(NSString *getRepeatLabel;)
{
// do something here with your string // maybe you must reload your table // it depends on where your returning data needs to display <--------Not sure what to do here
// NSDateFormatter*dateFormatter = [[NSDateFormatter alloc]init];
// NSArray*days = [dateFormatter shortWeekdaySymbols]; <------Here I would like when data is selected to show days in short symbol
NSLog (#"The selected day/s is %#", getRepeatLabel); <---nothing displaying on console
}];
}
}
#end
in FirstVC.h
#import <UIKit/UIKit.h>
#import "RootViewController.h"
typedef void(^WeekdayCompletionBlock)(NSString *dayName);
#interface FirstViewController : UITableViewController
{
NSString *dayName;
}
#property (nonatomic, strong) WeekdayCompletionBlock completionBlock;
#property (nonatomic,strong) NSString *dayName;
- (IBAction)save:(id)sender;
#end
in FirstVC.m
#import "FirstViewController.h"
#import "RootViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
#synthesize completionBlock;
#synthesize dayName;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Initialize table data
completionBlock = [NSArray arrayWithObjects:#"Sunday", #"Monday", #"Tuesday", #"Wednesday", #"Thursday", #"Friday", #"Saturday", nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [completionBlock count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"RepeatCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [completionBlock objectAtIndex:indexPath.row];
return cell;
}
// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog (#"The selected day/s is %#", [completionBlock objectAtIndex:indexPath.row]);
_getRepeatLabel = completionBlock; //<-----------string from RootVC gives error "undeclared _getRepeatLabel"
}
- (IBAction)save:(id)sender
{
NSUserDefaults *myNewWeekString = [NSUserDefaults standardUserDefaults];
[myNewWeekString setObject:completionBlock forKey:#"%#"];
[myNewWeekString synchronize];
self.completionBlock(myNewDayOfWeekString) <------error myNewDayOfWeekString undeclared and if i declare it here it complains about incompatibility
}
#end
Your code is a little bit wrong. I think you don't really understand block.
You want to pass more than string so the best way to do that is via array. Change block definition to accept array instead of string:
typedef void(^WeekdayCompletionBlock)(NSArray *dayName);
Change declaration of your property in FirstVC.h to:
#property (nonatomic, copy) NSArray *completionBlock; //This is your array, you use it as data source, It's not a block
//Add your block property
// This is your block property you will use it to pass the data between view controllers
#property (copy) WeekdayCompletionBlock returnBlock;
//Add property to keep your selected days
#property (nonatomic, strong) NSMutableArray *returnArray;
Add this line to viewDidLoad method:
self.returnArray = [[NSMutableArray alloc] init];
Change your didSelectRowAtIndexPath method in FirstVC.m to:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
cell.accessoryType = UITableViewCellAccessoryNone;
//remove data from array
[self.returnArray removeObject:[completionBlock objectAtIndex:indexPath.row]];
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
//add data to array
[self.returnArray addObject:[completionBlock objectAtIndex:indexPath.row]];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
In your save: method call block and pass value to rootVC replace line:
self.completionBlock(myNewDayOfWeekString);
with:
if (self.returnBlock)
{
self.returnBlock(self.returnArray);
}
[self.navigationController popViewControllerAnimated:YES];
The last change left to do is set up ablok in your rootVC.m file. Replace line:
[(FirstViewController *)destinationController setCompletionBlock:
with (nsstring needs to be replaced with nsarray - you pass array with all of the selected data)
[(FirstViewController *)destinationController setCompletionBlock:^(NSArray *getRepeatLabel)
You set up block not NSArray.
I don't know what are you trying to do here:
[myNewWeekString setObject:completionBlock forKey:#"%#"];
You are using %# as a key. It should be text for example #"MY_KEY_FOR_ACCESING_DAYSOFWEEK". You are saving completionBlock it's all of your days if you want to save just selected days replace it with self.returnArray.
Hope this help.

Casting to right object for tableView cells

I'm using tableView for displaying each tam tableView with different data.
when I coming back to the interface with all the tableView delegates i'm tantalizing the *list with a data (different arrays with fields) based on the the previous view
if(select = names )...
list = myData.Names;
if (select = workers)...
list = myData.Workers;
and so on...
and in the table delegates I use:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [list count];
}
But when I initializing the object for the table cells, How can I set exatcly the right object for the cell. now hard-coded I using:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MyIdentifier"] autorelease];
}
//HARD-CODED
names *s = [list objectAtIndex:indexPath.row];
cell.textLabel.text = names.firstName;
cell.detailTextLabel.text=s.lastName;
return cell;
}
When i setting the list to workers how can I know to cast every time to the right object? without writing millions of if?
for example if
if (select = workers)...
list = myData.Workers;
the [list count] will return the right number but i want to display data based on the list, how can i cast it so it automatically declare workers
workers *s = [list objectAtIndex:indexPath.row];
I think I understand your problem, so let me try to explain:
This can be easily solved using protocols and giving the cell configuring responsibility to the actual objects.
First, declare a protocol that your clases will conform to:
#class MyTableViewCell;
#protocol CellConfiguring
- (void)configureCell:(MyTableViewCell *)cell;
#end
Now make some classes and make sure all of them conform to this protocol:
#interface Workers : NSObject <CellConfiguring>
#property (retain, nonatomic) NSString *company;
#property (retain, nonatomic) NSString *salary;
#end
and:
#interface Names : NSObject <CellConfiguring>
#property (retain, nonatomic) NSString *firstName;
#property (retain, nonatomic) NSString *lastName;
#end
etc...
In their implementation, each of them will handle the configuring of the cell differently:
#import "MyTableViewCell.h"
#implementation Workers
- (void)configureCell:(MyTableViewCell *)cell
{
cell.textLabel.text = self.company;
cell.detailTextLabel.text = self.salary;
}
#end
and:
#import "MyTableViewCell.h"
#implementation Names
- (void)configureCell:(MyTableViewCell *)cell
{
cell.textLabel.text = self.firstName;
cell.detailTextLabel.text = self.lastName;
}
#end
Remember the cell you receive is a custom cell that you created. For instance:
#interface MyTableViewCell : UITableViewCell
#property (retain, nonatomic) UILabel *textLabel;
#property (retain, nonatomic) UILabel *detailTextLabel;
#end
Now, in your delegate call:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MyIdentifier"] autorelease];
}
id myObject = [list objectAtIndex:indexPath.row];
if ([myObject conformsToProtocol:#protocol(CellConfiguring)]
[myObject configureCell:cell];
return cell;
}
And that's it. No more if-else statements and no more casting.
Tip: Whenever you start seeing too many if-else statements in your class, dirtying up your code, start thinking about responsibilities. Should the object be in charge of figuring out how to configure the cell? Or could we delegate that task to another object?
Set One flag in .h file like BOOL flag. Data will be filled according flag.
Intially flag will be TRUE in ViewDidLoad.
Now set list like this:
if()...
listNames = myData.Names;
if ()...
listWorkers = myData.Workers;
and in the table delegates:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(flag){
return [listNames count];
else
{
return [listWorkers count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MyIdentifier"] autorelease];
}
if(flag)
{
names *s = [listNames objectAtIndex:indexPath.row];
cell.textLabel.text = names.firstName;
cell.detailTextLabel.text=s.lastName;
return cell;
}
else
{
names *s = [listWorkers objectAtIndex:indexPath.row];
cell.textLabel.text = names.firstName; // change for listWorkers
cell.detailTextLabel.text=s.lastName; // change for listWorkers
return cell;
}
}
Now when u want to change then
flag = FALSE; //worker data will be filled
[tableView reloadData]
You might be able to use the following solution ...
Create a custom object that conforms to UITableViewDataSource for each type of object. For example WorkerDataSource, NamesDataSource, etc...
Depending on the data you want to display, set the right dataSource for the tableView. An example from a project I work on ...
#define SECTION_HEADER_HEIGHT 20.0f
#interface FSMatchProgrammeDataSource ()
#property (nonatomic, copy) NSArray *matches;
#property (nonatomic, copy) NSArray *groupedMatches;
#end
#implementation FSMatchProgrammeDataSource
#synthesize matches = _matches;
#synthesize groupedMatches = _groupedMatches;
#synthesize didSelectMatchHandler = _didSelectMatchHandler;
+ (FSMatchProgrammeDataSource *)dataSourceWithMatches:(NSArray *)matches
{
FSMatchProgrammeDataSource *dataSource = [[FSMatchProgrammeDataSource alloc] init];
if (dataSource)
{
dataSource.matches = matches;
}
return dataSource;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return _groupedMatches.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *matches = [_groupedMatches objectAtIndex:section];
return matches.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MatchProgrammeCellIdentifier";
FSMatchProgrammeCell *cell = (FSMatchProgrammeCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[FSMatchProgrammeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSArray *matches = [_groupedMatches objectAtIndex:indexPath.section];
cell.match = [matches objectAtIndex:indexPath.row];
DLog(#"%#", cell.match);
return cell;
}
#pragma mark - Table view delegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// custom view created here ...
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return SECTION_HEADER_HEIGHT;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44.0f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (_didSelectMatchHandler)
{
NSArray *matches = [_groupedMatches objectAtIndex:indexPath.section];
FSMatch *match = [matches objectAtIndex:indexPath.row];
_didSelectMatchHandler(match);
}
}
#pragma mark - Properties
- (void)setMatches:(NSArray *)matches
{
// matches property set, also an sorted / grouped copy is created for display called groupMatches ...
}
#end
And the dataSource is assigned in the tableViewController like this:
- (void)refreshView
{
BOOL showInfoLabel = NO;
switch (_tabBar.selectedSegmentTag)
{
case TabProgramme:
_tableView.dataSource = _programmeDataSource;
_tableView.delegate = _programmeDataSource;
break;
case TabResults:
_tableView.dataSource = _resultsDataSource;
_tableView.delegate = _resultsDataSource;
break;
case TabStandings:
_tableView.dataSource = _standingsDataSource;
_tableView.delegate = _standingsDataSource;
break;
default: break;
}
[self.tableView reloadData];
}

Loading data into a uitableview in a uiviewcontroller

I am trying to load data from an array which was made by parsing json... I have the json data in the arrays for sure, but the table view methods are not even being called... I have the table view embedded in a regular uiviewcontroller, it compiles and everything the table just loads no data.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [transactions count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"any cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"any cell"] autorelease];
}
NSLog(#"got here");
cell.textLabel.text = [names objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [prices objectAtIndex:indexPath.row];
return cell;
}
- (void)viewDidLoad
{
[super viewDidLoad];
names = [[NSMutableArray alloc] init];
prices = [[NSMutableArray alloc] init];
[self requestTransactionData];
}
And here's my .h file...
#interface Controller1 : UIViewController
<UITableViewDelegate, UITableViewDataSource>
{
UIActivityIndicatorView *loadingIndicator;
IBOutlet UITableView *myTableView;
IBOutlet UILabel *totalLabel;
NSMutableArray *names;
NSArray *transactions;
NSMutableArray *prices;
}
#property(nonatomic, retain) IBOutlet UIActivityIndicatorView *loadingIndicator;
#property(nonatomic, retain) IBOutlet UILabel *totalLabel;
#property(nonatomic, retain) UITableView *myTableView;
I can't use ARC for this project. I also have it hooked up properly in the interface builder... These seems rather easy but for some reason I can't figure it out. Any suggestions?
Thanks
I don't see how you know the data source methods are not being called. You are not logging all of them. IfnumberOfRowsInSection is called before transactions is populated or is nil, it returns zero and that's the end of that.
Try calling reloadData using delayed performance so that it happens after transactions is populated. The table view doesn't magically know when your data is ready; you have to tell it.
call sef.tableView reloadData after names the prices were filled in requestTransactionData

Trouble Reloading UITableView

I am trying to create an iphone application that reads a json feed and I am having some trouble updating a TableView.
This is my TableViewController header file:
#interface LiveNewsController : UITableViewController <UITableViewDelegate, UITableViewDataSource>{
NSMutableData *responseData;
NSMutableArray *articleArray;
UITableView *tblView;
}
-(void) refreshPressed: (id)sender;
#property (nonatomic, retain) IBOutlet UITableView *tblView;
#end
These are some parts of my TableViewController implementation file:
#implementation LiveNewsController
#synthesize tblView;
-(id) init {
if(self = [super init])
{
self.title = #"Live News";
}
return self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"I am here %d",[articleArray count]);
return [articleArray count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSLog(#"cellForRowAtIndexPath called. Data size %d", [articleArray count]);
NSUInteger row = [indexPath row];
cell.textLabel.text = [articleArray objectAtIndex:row];
return cell;
}
-(void) refreshPressed: (id)sender
{
NSLog(#"Reload Pressed. Data size: %d", [articleArray count]);
[tblView reloadData];
}
Here is a screenshot of my .xib file with the connections:
So the main idea is the following:
Fetch articleArray
When refresh is pressed update view (the data is being fetched correctly but the snippet doesn't show it here)
numberOfRowsInSection and CellForRowAtIndexPath are only being called once when the view loads but nothing happens when I press the reload button.
I checked tblView and it is nil. I did some research and read that this is usually caused by some erroneous connections in the .xib file but I have triple checked that and I can't seem to pinpoint the problem.
Any help is appreciated,
Thanks!
You don't need to declare the UITableView tblView, the UITableViewController has already one (and therefore all subclasses too).
Just try [self.tableView reloadData];.

iPhone Multiple UITableView controls on one View

Im having a little issue with using more than 1 UITableView on a view.
Here's what I've done so far (using examples, etc from here and other places):
Created a class for each table. Each class is pretty basic:
.h:
#interface ConstructionDocumentsJobTable : UITableViewController <UITableViewDataSource, UITableViewDelegate> {
NSMutableArray *tableItems;
IBOutlet UITableView *itemsTable;
NSInteger recordSelected;
id <JobTableSelectionDelegate> tableSelectDelegate;
}
#property (nonatomic, retain) NSMutableArray *tableItems;
#property (nonatomic, assign) id <JobTableSelectionDelegate> tableSelectDelegate;
#end
.m:
#implementation ConstructionDocumentsJobTable
#synthesize tableItems, tableSelectDelegate;
#pragma mark -
#pragma mark View Life Cycle
-(void) loadView
{
}
-(void) dealloc
{
[tableItems release];
[super dealloc];
}
#pragma mark -
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tableItems count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [tableItems objectAtIndex:indexPath.row];
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//pass the tap value back to the delegate
}
Both are completely identical, save the names.
When Im making the call to the first one, it is called in the ViewDidLoad method of the controller of the view. It's pretty basic:
NSMutableArray *tableItems = [[NSMutableArray alloc] initWithCapacity:intMax];
//strDocumentType is set elsewhere and loaded here
if(strDocumentType == #"typea"){
[tableItems addObject:#"Type A - Value 1"];
[tableItems addObject:#"Type A - Value 2"];
}
else {
[tableItems addObject:#"Type B - Value 1"];
}
if(jobTableController == nil){
jobTableController = [[ConstructionDocumentsJobTable alloc] init];
[jobTableController loadView];
jobTableController.tableItems = tableItems;
jobTableController.tableSelectDelegate = self;
}
[tableJobList setDataSource:jobTableController];
[tableJobList setDelegate:jobTableController];
jobTableController.view = jobTableController.tableView;
The second table is built when a cell in the first table is selected. So, in the first tables selection method, the delegate is called back from the parent controller, which then has this:
NSMutableArray *tableTypeItems = [[NSMutableArray alloc] initWithCapacity:100];
if(tableSelect == #"plumbing"){
[tableTypeItems addObject:#"Something"];
[tableTypeItems addObject:#"Other"];
}
if(typeTableController == nil){
typeTableController = [[ConstructionDocumentsTypeTable alloc] init];
[typeTableController loadView];
typeTableController.tableItems = tableTypeItems;
typeTableController.tableSelectDelegate = self;
}
[tableTypeList setDataSource:typeTableController];
[tableTypeList setDelegate:typeTableController];
typeTableController.view = typeTableController.tableView;
[typeTableController.tableView reloadData];
//Code to move the first table off the screen and move this one into view goes here
Ive been stuck on this for days, and I really need to get this done!!!
Im sure it's something REALLLLLLY simple.
Any help you guys can pass along would be HUGELY appreciated.
Thanks everyone.
Your only defining itemsTable in your header, try defining another table, and in your cellForRowAtIndexPath try this:
if(itemsTable1){
/* stuff for first table */
} else {
/* stuff for second table */
}
And do the same for each UITableVIew Delegate