Multi-level UITableView Show - ios

My problem here is that i have a list of folders, and the folders directory will show on tableview, when user tap on folder(tableview cell)I need to jump into the next level of the folder and the tableview will display its sub files,(notice:not the tree structure,just show all its sub files, i get the folder's next level directory from a server when user tap the folder in cell) .In sub files list ,if user tap on a sub folder I need to show list of its sub files and so on.
my thought is to is create a subclass of UITableViewController that will recreate instances of itself when the user selects a row. put the first instance in a navigationcontroller and use
[navigationController pushViewController: animated] to push the multi levels one by one.
But i do not know how to override the method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
to achieve my thought. Thanks everyone ,i need your help!

I think this is the basic idea of what you want. Hopefully it gets you started anyway. Definitely read up on UITableViews and look at some sample code online or in a reference book.
#interface FileTableViewController <UITableViewController>
#property (nonatomic, retain) NSArray *filenames; // includes dirnames
#property (nonatomic, retain) NSArray *dirNames;
#end
#implementation FileTableViewController
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [filenames count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] dequeueReuseableCellWithIdentifier:#"cell"];
cell.textLabel.text = self.filenames[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *filename = self.filenames[indexPath.row];
if (self.dirnames containsObject:filename)
{
FileTableViewController *subVC = [[FileTableViewController alloc] init];
subVC.filenames = [self filenamesForSubDir:filename];
subVC.dirnnames = [self dirnamesForSubDir:filename];
[self.navigationController pushViewController:subVC animated:YES];
}
}

1> Just obtain the path of the next directory from didSelectRowAtIndexPath.
2> If you get response then replace the old array of data that is used in your cellForRowAtIndexPath for displaying the folder/files name with the data in response.
3> reload your table view and the delegates of table view will be called.(change delegate method according to your response).

Related

TableView in a TableViewCell

I want to put a tableview into a tableview cell. I like the formatted look it gives. I'm hoping to put in a few fields, for like name, email, etc. What am I missing to be able to make this work? Currently I can not set "ProfileCell" as the tableview class.
In my .h file of the profile cell I added:
#interface ProfileCell : UITableViewCell <UITableViewDataSource,UITableViewDelegate>
and in my .m file I added some basic methods for the tableview:
#import "ProfileCell.h"
#implementation ProfileCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Number of rows is the number of time zones in the region for the specified section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Doing this");
static NSString *MyIdentifier = #"MyReuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
return cell;
}
#end
EDIT:
I have seperated the code for the cellview tableview into it's own files, and linked tableview to this class, however it does not fire when viewed:
#interface ProfileBasicDetails : UITableView <UITableViewDataSource,UITableViewDelegate>
Per comments below
This is what my page should look like when said and done. It's so users can enter in their details, while looking nice and formatted, but not ghetto looking.
I'm assuming you've embedded the UITableView into the cell?
You should wire the tableview to an outlet in the cell. Then set the delegate on the tableview in your awakeFromNib method.
*As vikingosegundo mentioned in the comments, this is potentially a pretty fragile approach.

How to let the user reorder sections in a UITableView

I'm working on an app with stocks, arranged in portfolios. So this is a natural fit for the table view, and I'm working on the editing interaction; it's straightforward enough to allow the user to add or delete stocks, drag them around within one portfolio or to another portfolio, but one thing that I haven't been able to do gracefully is let the user drag one portfolio above or below another.
I've got a hacky solution right now, where row 0 of each section is the portfolio name, and if they drag that row above another portfolio, the whole table is reloaded with the portfolios switched. This works, but doesn't feel very natural.
I'm sure I'm not the first to encounter this problem; anyone have a more refined solution?
A related question - how do I let users create a new portfolio/section?
Easy peasy:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
{
NSMutableArray *_data;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_data = [NSMutableArray arrayWithObjects:#"One", #"Two", #"Three", nil];
self.tableView.editing = YES;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"reuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
}
cell.textLabel.text = _data[indexPath.row];
cell.showsReorderControl = YES;
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
[_data exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}
#end
EDIT:
What you've asked for now is a little more complicated. I created an example that puts tables into cells, which gives you nested cells. The example is highly unattractive, but it works, and there's no reason you can't make it look pretty, so check it out:
https://github.com/MichaelSnowden/TableViewInCell
If that doesn't work for you, try making UITableView moveSection:(NSInteger) toSection:(NSInteger) look pretty.
Documentation for that method is here.
My experience with the above method was that it's very easy to use, and it looks nice when it's called. A smart way to use it would be to create headers with tap gesture recognizers. On the first tap, highlight that section and record that indexPath, and on the second tap, call the method on the two index paths. It should work nicely, but you won't get drag-and-drop from it.

UITableView - cellForRowAtIndexPath Is Not Being Called [duplicate]

This question already has answers here:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is not being called
(4 answers)
Closed 9 years ago.
I know the title is illustrating enough, and I KNOW that this question has been ask a couple of times, but in this case I couldn't manage to make it work.
I already have worked with UITableViews and they all have been working fine, but this time, I even checked my code with other working copies, but it is not working.
So, here's the story :
I have a table view in one of my view controller's in storyboard with a custom class named ContactViewController, and it is connected to code using an outlet named favContactsTable
Here are related parts from its .h and .m :
ContactViewController.h
#interface ContactViewController : UIViewController <UITableViewDataSource>
// Other Stuff
.
.
#property (retain, nonatomic) IBOutlet UITableView *favContactsTable;
.
.
// Other Stuff
#end
And here is implemented functions of UITableViewDataSource protocol :
ContactViewController.m
// Rest of the code
.
.
.
.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"TEST OUTPUT");
//Test
int a = 1; // Breakpoint
a++;
cell = [self.favContactsTable dequeueReusableCellWithIdentifier:#"FavContactCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"FavContactCell"];
}
cell.textLabel.text = #"FAV";
.
.
.
}
I have another button in my view controller that reloads data for this table, but when I press it none of the breakpoints is triggered and no log is printed, none of the functions are being called. Here's the code for that button :
- (IBAction)refreshTables:(id)sender
{
[self.favContactsTable reloadData];
}
I checked almost every part with my other projects that has a dynamic table view like this, and I can't figure out what's wrong.
Please do not mention other similar questions as duplicates, because I already have read them all ( or at least, most of them ) and I'm sure I'm not returning 0 rows for each section, and I'm sure favContactsTable is not nil when I send reloadData message to it and ... etc !
Any idea that may help is really appreciated.
#interface myViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
}
set delegate
drag to your viewcontroller icon
check your cell identifier as same in your code also
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SimpleTableCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:#"SimpleTableCell"];
}
return cell;
}
try this code :
#interface ContactViewController : UIViewController <UITableViewDataSource, UITableViewDelegate >
.
.
.
and then in viewDidLoad add delegate to your tableview property example :
self.favContactsTable.delegate = self;
You did not confirmed these protocols :UITableViewDataSource, UITableViewDelegate.Add this to your file.
and after that:
favContactsTable.delegate=self;
favContactsTable.datasource=self;
and attach directly from your Xib file.

NSArray behaving Weird Objective C

I am from C# background and now learning objective C.
I created two NSArrays 1.optionsLabelArray and 2.optionsLabelSubtitleArray initially with two elements each (optionsLabelArray=>Yes & No) and optionsLabelSubtitleArray(acceptable and action). Now requirement changed and I have to add one more item and I am doing it like below. But It still shows only two elements.
I tried using NSMutableArray , no luck. Looks like it's a small issue but couldn't figure it out.. any ideas will be appreciated.
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
optionsLabelArray = [[NSArray alloc] initWithObjects:#"Yes", #"No",#"No" ,nil]; // here is problem
optionsLabelSubtitleArray = [[NSArray alloc] initWithObjects:#"Acceptable",#"Action required",#"No", nil]; // here to
static NSString *CellIdentifier = #"CheckerCell";
CustomCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.optionLabel.text = [optionsLabelArray objectAtIndex:indexPath.row];
cell.optionLabelSubtitle.text = [optionsLabelSubtitleArray objectAtIndex:indexPath.row];
return cell;
}
You need to update the implementation of your - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section method to support more rows. To do so you probably want to have an implementation where the array is either an instance variable of the class or otherwise consistent between the two methods.
you need implement following method and return 3 when you increase cell count
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
see more on https://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDataSource/tableView:numberOfRowsInSection:

How to add submenu's in table view in ios

i don no whether this question asked or not but i still searching for an answer.Am working in tableview concept and i am creating a table view like with some set of menu's like inbox, sent,setting etc.Now what i want is i want to create sub menu's inside every menu, for example if i click inbox it should show new,replied,deleted etc like this for every main menu i want to create sub menu. Using array we can load by checking section but with out using array i want to create directly and notable one i have used custom cell i also want to show images as per the menu if it is inbox i have to show inbox image. Could any one help me??
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 8;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 8;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentifier=#"ViewProfileCell";
MyHomeViewCell *cell= [[MyHomeViewCell alloc] init];
cell=(MyHomeViewCell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if(!cell)
{
NSArray *nibofMyHomeCell=[[NSBundle mainBundle]loadNibNamed:#"MyHomeViewCell" owner:self options:Nil];
cell=[nibofMyHomeCell objectAtIndex:0];
}
if(indexPath.section==0)
{
cell.MyHomeMenuLabel.text=#"Inbox";
}
}
Here ara a good tuts it could help you:
Expanding/Collapsing TableView Sections
Expandable UITableView
And this github awesome code here by Oliver Letterer.
You can create a UITableview which will contain cells on it as you said inbox, sent, setting etc.
After you need to create another UITableView which will contain your submenu buttons or labels like new,replied, deleted when click on inbox.
Simillarly you will do for rest of the cells in your main UITableView.
And don't get confuse on how will i identify which tableview will get called.
You will check for tableview name like below:
if(tableView==main)
{
///Code for main menu tableview.
}
else if(tableView==sub)
{
////Code for submenu tableview.
}
This you will do in all UITableView Delegate and Datasource methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
There are many ways you can do this. You could for example show each item as a section header and if the section header is tapped you show (or hide) all of the rows in that section. The rows being the 'sub menu' items (new, replied, deleted). Or you could use different sections for each part and how or hide the sections. Or show and hide rows. This is pretty much all controlled by changing the section and row counts.

Resources