In my application, I have done Expanded table view cell inside that again I need to expand the cell.I Am facing the problem with again inserting expanded cell.Is it possible to do?
//cellForRowAtIndexPath:
[cell.expandButton addTarget:self action:#selector(expandAction:) forControlEvents:UIControlEventTouchUpInside];
if (indexPath.item < [array count] + 1) {
NSLog(#"Show one cell”);
}else{
NSLog(#"Show another cell")
}
//Button action
-(void) expandAction:(UIButton*)sender{
if (indexPath.item < [array count] + 1) {
NSLog(#"Show one cell”);
}else{
NSLog(#"Show another cell")
}
}
How do you generate your tableView data? If you use array of objects, then you could make it kind of like that:
CellItem class saves cell state
#interface CellItem : NSObject
#property BOOL isExpanded;
#end
Your custom cell class. Setup method customize your cell depending on CellItem object state
#interface ExpandableCell : UITableViewCell
- (void)setupWith:(CellItem *)cellItem;
#end
#implementation ExpandableCell
- (void)setupWith:(CellItem *)cellItem {
if(cellItem.isExpanded) {
//Expand your cell
} else {
//Don't exp and
}
}
#end
Array of objects where you get data for your tableView
#property NSArray<CellItem *> *items;
Protocol, which defines methods for cell state delegate. Implement this methods in class, which is also delegate of yourtableView. Call this method from cell everytime user expand / close it. In implementation save cellItem in items arrays.
#protocol ExpandableCellDelegate
- (void)cellDidChangeStateWithCellItem: (CellItem *)cellItem;
#end
UITableView cells are reusable, so everytime cell appears on a screen you should customize it again.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CellItem *cellItem = self.items[indexPath.row];
ExpandableCell *cell = (ExpandableCell *)[tableView dequeueReusableCellWithIdentifier:#"InsertYourCEllID"];
[cell setupWith:cellItem];
return cell;
}
Related
I have a form in my app that exists out of a UITableView with custom cells. These cells can contain a UITextField, UISegmentedControl or a UISwitch. This is how I set this up:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableViewInner cellForRowAtIndexPath:(NSIndexPath *)indexPath {
DetailTableViewCell *cell;
static NSString *MyIdentifier = #"MyIdentifier";
DetailTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[DetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
[cell setTextField:#"John Appleseed"];
// or
[cell setSegment];
[cell setSegmentIndex:1];
// or
[cell setSwitch];
[cell setSwitchEnabled:YES];
return cell;
}
Now, when a user taps the save button I need to fetch all this information and init a model with it, like this:
[[Restaurant alloc] initWithName:#"Name here" withNotifications:1 withFrequency:1 withDate:#"Date here" andWithDistance:#"Distance here"];
What's the best and cleanest way possible to convert all these inputs to data in my model? I feel like looping over all the cells is a bit over the top.
like looping over all the cells is a bit over the top
It isn't just over the top: it's totally wrong. The data doesn't live in the cells; it lives in the data. Model, view, controller; the cell is just view! Its job is to represent the model (the data). There should be nothing to loop over, therefore; you should already have the data as data.
Now, when a user taps the save button I need to fetch all this information
Actually, what I would do is capture the information when the user makes the change. Give the text field, switch, or segmented control a control action-target so that a message is sent to you telling you that something happened (e.g. the switch value changed, the text was edited, and so on) and capture the data right then.
The only question then becomes: I've received a message from a control: what row of the table is it in? To find out, walk the hierarchy up from the control until you come to the cell, and then ask the table what row this cell represents:
UIView* v = sender; // the control
do {
v = v.superview;
} while (![v isKindOfClass: [UITableViewCell class]]);
UITableViewCell* cell = (UITableViewCell*)v;
NSIndexPath* ip = [self.tableView indexPathForCell:cell];
A more cleaner approach using custom blocks.
DetailTableViewCell.h
typedef void (^ saveBlock_block_t )(WhateverYourReturnObjectType *obj);
#interface DetailTableViewCell : UITableViewCell
- (void)configureCell:(NSString *)textFieldVal
cellBlock:(saveBlock_block_t)cellBlock;
#end
DetailTableViewCell.m
#interface DetailTableViewCell ()
{
#property (copy, nonatomic) saveBlock_block_t saveBlock;
}
#end
#implementation DetailTableViewCell
- (void)configureCell:(NSString *)textFieldVal
cellBlock:(saveBlock_block_t)cellBlock
{
[cell setTextField: textFieldVal];
[self setSaveBlock:cellBlock];
}
-(IBAction)saveButtonAction:(id)sender //Action on your save button
{
self.cellBlock(obj); // Whatever object you want to return to class having your table object
}
#end
Then from cellForRowAtIndexPath call it as -
[cell configureCell:#"John Appleseed”
cellBlock:^(WhateverYourReturnObjectType *obj){
//Do what you want to do with 'obj' which is returned by block instance in the cell
}];
I am using a Storyboard's prototype cell with a custom table cell class and the UILabels are nil in cellForRowAtIndexPath. Xcode's Identifier is correct, the cell is initialized, and the default UILabels (i.e. textLabel and detailTextLabel) are initialized but not the custom UILabels I added and setup IBOutlets for. A couple things I have tried:
I tried removing the registerClass calls in ViewDidLoad but that crashes since the cells need it to be initialized as the custom class GenericDetailCell.
viewWithTag returns nil
I tried iterating through all the subviews of UITableView to see if I was getting the wrong cell. The correct cell is getting returned by dequeueReusableCellWithIdentifier
Has anyone run into this?
#implementation PeopleGroupPickerViewController
{
NSArray *people;
NSArray *searchResults;
}
- (void)viewDidLoad
{
[super viewDidLoad];
people = [[DAL sharedInstance] getPeople:false];
[self.tableView registerClass:[GenericDetailCell class] forCellReuseIdentifier:#"PersonCell"];
[self.searchDisplayController.searchResultsTableView registerClass:[GenericDetailCell class] forCellReuseIdentifier:#"PersonCell"];
// stackoverflow.com/questions/5474529
[self.searchDisplayController setActive:YES];
[self.searchDisplayController.searchBar becomeFirstResponder];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(tableView == self.searchDisplayController.searchResultsTableView)
{
return 1;
}
else
{
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView == self.searchDisplayController.searchResultsTableView)
{
return searchResults.count;
}
else
{
return people.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
GenericDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:#"PersonCell" forIndexPath:indexPath];
Person_ *thisPerson;
if(tableView == self.searchDisplayController.searchResultsTableView)
{
thisPerson = (Person_ *) searchResults[indexPath.row];
NSLog(#"searchResultsTableView, %#",thisPerson.sName);
}
else
{
thisPerson = (Person_ *) people[indexPath.row];
NSLog(#"UITableView, %#",thisPerson.sName);
}
Person_ *thisSpouse = [[DAL sharedInstance] getSpouse:thisPerson People:people];
// cell.fieldName and cell.fieldValue are nil, cell is not nil
cell.fieldName.text = thisPerson.sName;
cell.fieldValue.text = thisSpouse.sName;
return cell;
}
GenericDetailCell.h:
#interface GenericDetailCell : UITableViewCell
#property (nonatomic,weak) IBOutlet UILabel *fieldName;
#property (nonatomic,weak) IBOutlet UILabel *fieldValue;
#end
Seems to me you might be attempting to combine a UITableViewCell predefined style (e.g. UITableViewCellStyleSubtitle) with a custom cell.
In your storyboard, if you have selected a predefined style for your Prototype cell, other controls will not be recognised.
To remedy this problem, in the Attribute Inspector for your Prototype UITableViewCell, select "Custom" type. Then add into the Prototype cell all the controls you require, including those needed to replace the default controls previously added automatically into the predefined cell type.
I have a view which contain a UITableView. The cells of this UITableView are created in Interface Builder, in order to have different kinds of cells. So the action of each button is managed in the cell classes like following.
- My UITableView containing different kinds of cells :
- The header file of my class for type one cells ("CellTypeOne.h") :
#interface CellTypeOne : UITableViewCell
{
}
- (IBAction)actionForFirstButton:(id)sender;
- (IBAction)actionForSecondButton:(id)sender;
- (IBAction)actionForThirdButton:(id)sender;
- (IBAction)actionForFourthButton:(id)sender;
- The header file of my class for type two cells ("CellTypeTwo.h") :
#interface CellTypeTwo : UITableViewCell
{
}
- (IBAction)actionForTheUniqueButton:(id)sender;
- The view which contain my table view ("ViewContainingMyTableView.h") :
#interface ViewContainingMyTableView : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
UITableView *myTBV;
}
#property (retain, nonatomic) IBOutlet UITableView *myTBV;
#end
Here the thing I want to do :
When I click on the first button in the first cell, for example, I want to be able to show the indexPath of the "current" cell.
For example, I want to have the following output when :
I click on the first button in the first cell : 0
I click on the third button in the first cell : 0
I click on the first and unique button in the second cell : 1
etc...
since u are using the custom cell i think u need to handle selection also, because u are touching the button inside the custom cell not the cell itself therefore tableview delegate methods are not fired, better as i said in ur custom cell put a delegate method for example in ur custom cells
in your CellTypeOne.h add this
//#class CellTypeOne; //if u want t pass cell to controller
#protocol TouchDelegateForCell1 <NSObject> //this delegate is fired each time you clicked the cell
- (void)touchedTheCell:(UIButton *)button;
//- (void) touchedTheCell:(CellTypeOne *)cell; //if u want t send entire cell this may give error add `#class CellTypeOne;` at the beginning
#end
#interface CellTypeOne : UITableViewCell
{
}
#property(nonatomic, assign)id<TouchDelegateForCell1> delegate; //defining the delegate
- (IBAction)actionForFirstButton:(id)sender;
- (IBAction)actionForSecondButton:(id)sender;
- (IBAction)actionForThirdButton:(id)sender;
- (IBAction)actionForFourthButton:(id)sender;
in your CellTypeOne.m file
#synthesize delegate; //synthesize the delegate
- (IBAction)actionForFirstButton:(UIButton *)sender
{
//add this condition to all the actions becz u need to get the index path of tapped cell contains the button
if([self.delegate respondsToSelector:#selector(touchedTheCell:)])
{
[self.delegate touchedTheCell:sender];
//or u can send the whole cell itself
//for example for passing the cell itself
//[self.delegate touchedTheCell:self]; //while at the defining the delegate u must change the sender type to - (void)touchedTheCell:(CellTypeOne *)myCell; if it shows any error in the defining of the delegate add "#class CellTypeOne;" above the defying the delegate
}
}
and in your ViewContainingMyTableView.h
#interface ViewContainingMyTableView : UIViewController <UITableViewDelegate, UITableViewDataSource ,TouchDelegateForCell1> //confirms to custom delegate like table delegates
{
UITableView *myTBV;
}
#property (retain, nonatomic) IBOutlet UITableView *myTBV;
#end
and in the ViewContainingMyTableView.m file
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//during the creating the custom cell
CellTypeOne *cell1 = [self.aTableView dequeueReusableCellWithIdentifier:#"cell"];
if(cell1 == nil)
{
cell1 = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.delegate = self; //should set the delegate to self otherwise delegate methods does not called this step is important
}
//now implement the delegate method , in this method u can get the indexpath of selected cell
- (void)touchedTheCell:(UIButton *)button
{
NSIndexPath *indexPath = [self.aTableView indexPathForCell:(UITableViewCell *)button.superview];
NSLog(#"%#",indexPath.description);
}
/* if u pass the cell itself then the delegate method would be like below
- (void)touchedTheCell:(CellTypeOne *)myCell
{
NSIndexPath *indexPath = [self.aTableView indexPathForCell:myCell];//directly get the cell's index path
//now by using the tag or properties, whatever u can access the contents of the cell
UIButton *myButton = [myCell.contentView viewWithTag:1000]; //get the button
//... u can access all the contents in cell
}
*/
in your case, this is for first button in the cell, add the delegate methods for each buttons having different functions, repeat above procedure for another buttons in the cell
hope u get this :) hapy coding
With iOS7 you can get it like this :
- (IBAction)actionForTheUniqueButton:(id)sender
{
YouCellClass *clickedCell = (YouCellClass*)[[sender superview] superview];
NSIndexPath *indexPathCell = [self.tableView indexPathForCell:clickedCell];
}
You may use tag inside your xib on each cell to differentiate them.
Use after [self tag] inside the cell class
create tableView IBOutlet in .h or .m file. You can get the indexpath of selected tablecell by using
inside ur actionForFirstButton: method
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSLog(#"I click on the first button in the first cell : %d", indexPath.row);
edited : use tag of the button to get the index path , in your actionForFirstButton method
if(sender.tag == 0 || 1 || 2 || 3){
{
NSLog(#"the cell is : %d", 0);
}
else if(sender.tag == 4){
{
NSLog(#"the cell is : %d", 1);
}
else{
NSLog(#"the cell is : %d", 2);
}
Your requirement is just to get back the corresponding row value of a button...
Then you have a very simple way ,
#interface CellTypeOne : UITableViewCell
{
NSNumber *rowval;
}
#property (nonatomic,retain)NSNumber* rowval;
same thing in your CellTypeTwo.h
#interface CellTypeTwo : UITableViewCell
{
NSNumber *rowval;
}
#property (nonatomic,retain)NSNumber* rowval;
and now in both .m file you must have defined a method for buttons, So with in those method
just paste this line
NSLog(#"Number : %d",[rowval integerValue]);
and within )tableView cellForRowAtIndexPath: method add this line too
cell.rowval = [[NSNumber alloc]initWithInt:indexPath.row ];
Now every time you press a button of different cell it will reply back with row value of the cell
Hope this will help.. :-)
Im writing an app where someone adds a contact to the app, giving their name, number and photo. Then this information is displayed in a table, with each individual contact on a different cell and when the user presses on the cell it will call the number that was typed in for the contact. I have put in a large button on each of the cells for the user to press. This is the code
PictureListMainTable.m
#import "PictureListMainTable.h"
#import "PictureListDetail.h"
#import "CoreDataHelper.h"
#import "Pictures.h"
#implementation PictureListMainTable
#synthesize managedObjectContext, pictureListData, callButton;
// When the view reappears, read new data for table
- (void)viewWillAppear:(BOOL)animated
{
// Repopulate the array with new table data
[self readDataForTable];
}
// Grab data for table - this will be used whenever the list appears or reappears after an add/edit
- (void)readDataForTable
{
// Grab the data
pictureListData = [CoreDataHelper getObjectsForEntity:#"Pictures" withSortKey:#"title" andSortAscending:YES andContext:managedObjectContext];
// Force table refresh
[self.tableView reloadData];
}
#pragma mark - Actions
// Button to log out of app (dismiss the modal view!)
- (IBAction)logoutButtonPressed:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}
#pragma mark - Segue methods
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get a reference to our detail view
PictureListDetail *pld = (PictureListDetail *)[segue destinationViewController];
// Pass the managed object context to the destination view controller
pld.managedObjectContext = managedObjectContext;
// If we are editing a picture we need to pass some stuff, so check the segue title first
if ([[segue identifier] isEqualToString:#"EditPicture"])
{
// Get the row we selected to view
NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
// Pass the picture object from the table that we want to view
pld.currentPicture = [pictureListData objectAtIndex:selectedIndex];
}
}
#pragma mark - Table view data source
// Return the number of sections in the table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// Return the number of rows in the section (the amount of items in our array)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [pictureListData count];
}
// Create / reuse a table cell and configure it for display
- (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];
}
// Get the core data object we need to use to populate this table cell
Pictures *currentCell = [pictureListData objectAtIndex:indexPath.row];
// Fill in the cell contents
cell.textLabel.text = [currentCell title];
cell.detailTextLabel.text = [currentCell desc];
int number;
number = [currentCell desc];
-(IBAction)MakePhoneCall:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:",number]];
}
// If a picture exists then use it
if ([currentCell smallPicture])
{
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.imageView.image = [UIImage imageWithData:[currentCell smallPicture]];
}
else{
}
return cell;
}
// Swipe to delete has been used. Remove the table item
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Get a reference to the table item in our data array
Pictures *itemToDelete = [self.pictureListData objectAtIndex:indexPath.row];
// Delete the item in Core Data
[self.managedObjectContext deleteObject:itemToDelete];
// Remove the item from our array
[pictureListData removeObjectAtIndex:indexPath.row];
// Commit the deletion in core data
NSError *error;
if (![self.managedObjectContext save:&error])
NSLog(#"Failed to delete picture item with error: %#", [error domain]);
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
-(IBAction)MakePhoneCall:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:",number]];
}
#end
PictureListMainTable.h
#import <UIKit/UIKit.h>
#interface PictureListMainTable : UITableViewController
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (strong, nonatomic) NSMutableArray *pictureListData;
#property (nonatomic, retain) IBOutlet UIButton *callButton;
-(IBAction)MakePhoneCall:(id)sender;
- (void)readDataForTable;
#end
Where should I place the IBaction and why isint it working at the moment where it is and how can I make it work?
There are a couple of approaches you could take to achieve this. But firstly, I don't understand what you are doing at the bottom of -tableview:cellForRowAtIndexPath:. It's as if you are trying to define your IBAction method inside this method. You also have it defined at the bottom of the implementation, but in that method the number variable is not in scope.
Anyway, you should subclass the UITableViewCell. In the implementation for the subclass, you should define the IBAction method and hook it up in interface builder, or otherwise.
When the button is tapped, you should hand the number for the selected cell back to the PictureListMainTable view controller, in order for that view controller to process it (i.e. call the number). You can do this in two ways:
1) the delegate method
Create a protocol, defined in the header file for your subclass of UITableViewCell. And make the main view controller conform to this protocol. Set the cell's delegate to the main view controller. In the implementation of the cell subclass, call this delegate method. For example:
the header file for the UITableViewCell subclass "PictureListMainTableCell.h"
#protocol PictureListMainTableCellDelegate;
#interface PictureListMainTableCell : UITableViewCell
#property (nonatomic, copy) NSString *telephoneNumber;
#property (nonatomic, weak) id<PictureListMainTableCellDelegate> delegate;
#end
#protocol PictureListMainTableCellDelegate
-(void)pictureListMainTableCell:(PictureListMainTableCell *)cell wantsToCallNumber:(NSString *)number;
#end
the implementation file "PictureListMainTableCell.m"
#import "PictureListMainTableCell.h"
#implementation PictureListMainTableCell
-(IBAction)MakePhoneCall:(id)sender
{
//send the delegate the number to call.
[self.delegate pictureListMainTableCell:self wantsToCallNumber:self.telephoneNumber];
}
#end
Above, in the MakePhoneCall method, we call -pictureListMainTableCell:wantsToCallNumber: on the delegate. In this case, the delegate is your main view controller. We will set this below.
Setting the cell's delegate: In your main view controller file (PictureListMainTable.m), in the -tableView:cellForRowAtIndexPath: method, set the delegate on the cell to self. e.g.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// get the cell...
PictureListMainTableCell *cell = // dequeue the cell
// do some other setting up...
// set the delegate on the cell
cell.delegate = self;
// set the telephoneNumber variable on the cell, for example...
cell.telephoneNumber = [currentCell desc];
return cell;
}
Now you need to make sure self implements the delegate method. So still in PictureListMainTable.m, you need to define the method as follows:
#pragma mark - PictureListMainTableCellDelegate methods
-(void)pictureListMainTableCell:(PictureListMainTableCell *)cell wantsToCallNumber:(NSString *)number
{
NSString *urlString = [NSString stringWithFormat:#"tel://%#", number];
NSLog(#"calling telephone number [%#]", number);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}
You should also specify that the PictureListMainTable class conforms to your new protocol, as well as the UITableViewDataSource protocol. Add a private category on PictureListMainTable as follows (at the top of the implementation file, after the imports, before #implementation):
#interface PictureListMainTable () <UITableViewDataSource, PictureListMainTableCellDelegate>
#end
(this extends the PictureListMainTable interface. It only extends it to specify privately that it conforms to these protocols.)
2) the NSNotification method
While I was typing out the above explanation, I decided it's my preferred way of doing things, so I would recommend doing it like that. There is the option of posting an NSNotification form your cell subclass, and observing for this notification from your main view controller. Just look into NSNotificationCenter, the following methods:
–postNotificationName:object:userInfo: (send the number in userInfo dictionary). Listen for it using –addObserver:selector:name:object:.
But like I said, option 1 is better, in my opinion.
Let me know if anything is unclear, good luck :)
EDIT: I really recommend reading this blog post to understand delegation: http://alexefish.com/post/15966868557/understanding-and-creating-delegates-in-objective-c
In my iPad app storyboard design, there are two table views added.
One table view is for displaying the folder names and the other table view is for displaying the file names. When ever a folder table view cell is selected the files inside the selected folder needs to to be displayed in the other table view(the files table view).
My problem is
I am confused about
How to add delegate and data source for each table view in ViewController ? or is it possible to add data source and delegates for each table view in a custom class other than ViewController ?
and
How to handle the selection of cell ?
Please help !
First off: Why don't you just display the files on a pushed viewController which takes up the whole screen? Seems more intuitive for me.
If you want to do it with two tableViews, and assuming they use dynamic cells than:
1, In the .h file of your view controller
specify two tableView properties like so:
#property (weak, nonatomic) IBOutlet UITableView *foldersTableView;
#property (weak, nonatomic) IBOutlet UITableView *filesTableView;
implement the two delegate protocols of the UITableVIew
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
2, Add the two UITableViews onto your ViewController, and...
...link your Outlets to the two tableviews
...on the attributes inspector set the Tag of the foldersTableView to 1 and the filesTableView to 2
...select each of the UITableViews, go to the Connections Inspector and link their two delegate methods (delegate and datasource) to your ViewController (for both of them)
3, In the .m file of your ViewController implement the three datasource methods of UITableView like so:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if (tableView.tag == 1) {
return theNumberOfSectionsYouWantForTheFolderTableView;
} else {
return theNumberOfSectionsYouWantForTheFilesTableView;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView.tag == 1) {
return [foldersArray count];
} else {
return [filesArray count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.tag == 1) {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
return cell;
} else {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
return cell;
}
}
4, Implement the selection:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.tag == 1) {
//fill filesArray with the objects you want to display in the filesTableView...
[filesTableView reloaddata];
} else {
//do something with the selected file
}
}
Hope I got everything correctly. Don't forget to #synthesize the properties in the .m file if you are using pre XCode 4.4.
If you are using multiple tables, don't forget to hide as appropriate:
- (void)viewWillAppear:(BOOL)animated
{
[self.table2 setHidden:YES];
[self.table1 setHidden:NO];
// Probably reverse these in didSelectRow
}