Create a dynamically sized table - ios

I want to create a dynamic list just like the one in Slack, specifically the Channels, which updates with which channels you're a part of:
However, I have no idea where to start. While I'm using the SWRevealViewController Library, it only has a static table in the examples, not a dynamic one. While I'm working through this apple documentation, it's still not specific to interacting with SWRevealViewController, which I would prefer, although I certainly wouldn't mind a non-SWRevealViewController method. I'd definitely appreciate some tips to head in the right direction! All my view controllers show up properly right now, so I'm in the process of adding information to them.
Somewhat related:
Set UITableView Delegate and DataSource
UITableView issue when using separate delegate/dataSource
Creating A TableVIew Programmatically With Objective-C iOS

You may use this tutorial to create such menu:
http://www.appcoda.com/ios-programming-sidebar-navigation-menu/
You have only to substitute those static UITableViewController with dynamic.
You may easily find how to create dynamic. Its body will be something like this one:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return menuItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}

Related

iOS UITableViewCell title slides completely out of view when editing

I have a simple UITableView
I'm doing my first Edit implementation, to add Delete functionality. Most of this stuff is the default apparently. Pretty much the only method I added to my Controller is
- (void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
Program *victim = Site.current.sortedPrograms[indexPath.row];
[Site.current removeProgram: victim];
[self.tableView
deleteRowsAtIndexPaths: #[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
When I hit the Edit button, I see
which is good. But When I hit the minus button on one with a shorter title, I see the following
The title is completely scrolled out of site! Similarly, if I use the iOS 7 swipe left gesture, even on a longer title, the title is scrolled out of view:
The code I use to create the cell is pretty simple:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ProgramCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Program *program = Site.current.sortedPrograms[indexPath.row];
cell.textLabel.text = program.name;
cell.detailTextLabel.text = [program.script prettyCycleDuration];
return cell;
}
This isn't what I want my users to see. I would rather see it scroll the title only if necessary, so that context (not whitespace) associates with the delete button. I am just using a stock UITableViewCell (not a subclass), and I'm setting the 'style' to Subtitle. I've looked at the delegate methods, and a book I have, but I don't see anything that shows how to deal with this. Is there a way? Will I need to do my own Cell subclass and handle the states more explicitly? Or is there something else I can do?
Update
Even after converting to a custom subclass, with constraint based layouts for my cell, it still slides the whole view over, rather than compress it.

How to Initialize my UITableView table to have a certain number of rows based on input from user and a Button

I'm really new to Objective-C here so what I'm asking may be trivial to most of you but any guidance will help.
Here's a picture of my storyboad.
My current objective is to allow for the user to enter in the number of sets (NSInteger *numReps) and then press the "Log Reps" button and have the table initialize with numReps cells that look like the prototype cell.
Now where I'm at a loss for the implementation. I've never done this kind of thing before so I'm not exactly sure what the best way to go about it is. I have thought of making a custom class for the UITableView table that would take info from the view after the Log Reps button is pushed. I'm not entirely sure how this would need to be implemented. Or can I simply add the table to the properties of the view controller and setup the table within the view controller? That was my initial idea and seems ideal so that I would have everything in one place.
Pleas advise. I am new to all of this and come from a C++ background so I'm still learning a lot of the notation.
Try this:
-(IBAction)btnLogClicked {
int iSet = 4 //Number of row in table
UITableView *tblView= [[UITab;eView alloc]initWithFrame:CGRectMake(0,50,320,100)];
tblView.delegate = self;
tblView.dataSource = self;
[self.view addSubView:tblView];
}
Table View Data Source and Delegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return iSet;
}
- (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];
}
// Display what you want to display in each cell
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
It's not clear what you want to present in your prototype cells, but you need an array (or probably an array of dictionaries) to populate the cells. The number of rows is determined by the number of entires (the count) of that array. So, if you take the number entered by the user, and add that many object to your array, then call reloadData on the table, you will get the number of rows that you want. What those object are that you add to the array, depends on what you're trying to show there.
you could start reading: Table View Programming Guide for iOS
But I can answer you:
You can add the UITableView to the UIViewController, but you need set your UIViewController like the TableView's delegate and dataSource. Your ViewController need to implement the protocol: UITableViewDataSource
The method that you are looking for is: tableView:numberOfRowsInSection:
But I really recommend you that read the Apple Reference.

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.

What is the best way to implement one data source that services multiple table views in iOS

I have three UITableView's and they all need access to essentially the same data. The only difference is how each table view formats the data. What is the best way to implement this? I was thinking that if each table view had a tag of some sort, I could reference that from my data source when sending back the UITableViewCell's.
Each of the datasource calls passes in the UITableView that is requesting the information. Comparing this tableview to your three, you can determine which one it is and format the data differently. I would probably implement separate methods for each tableview and, pass the datasource method to the appropriate method. The other method would then create the customized cell creation and setup. eg for cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == [self tableView1])
return [self tableView1:tableView cellForRowAtIndexPath:indexPath];
if (tableView == [self tableView2])
return [self tableView2:tableView cellForRowAtIndexPath:indexPath];
if (tableView == [self tableView3])
return [self tableView3:tableView cellForRowAtIndexPath:indexPath];
Assert0(NO, #"UITableView not recognized!");
return nil;
}
- (UITableViewCell *)tableView2:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Do normal cell creation for tableView1 here
}
I don't know if there's any gotchas off-hand for running multiple UITableViews off of the same datasource, so tread lightly.
look into singleton design pattern - here is an example on how to implement for iOS.

Best approach: Displaying a "nothing here, yet" screen instead of empty tableView cells

When opening a UITableViewController, I'd like to display a screen saying "nothing here, yet" (c.f. screenshot of the Dropbox App below) instead of empty rows when there are no sections/rows. This is done in multiple Apps already, but I'd like to know if there is a recommended way of achieving this.
I can imagine multiple ways of doing this, including
adding a UIView as a subView to the view of the tableViewController
switching to a UIViewController and implementing the tableView behavior on my own
setting the UIView as the backgroundView and hiding the tableView cells
Thank you for all your ideas!
The way I usually do it, is displaying a single custom cell when there's nothing to display.
There is a new (as of June 2014) library called DZNEmptyDataSet handling exactly this case. It uses the first approach (UIView as a subview) described in the question. Pretty neat realisation.
You must be having some data array through which you can define few things, in table view.. You can achieve this in your tableview only with its delegate methods..
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if([dataArr count] == 0)
return 1;
return [dataArr count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if([dataArr count]==0){
return self.view.frame.size.height;
}
return 44.0;
}
and then
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if([dataArr count]==0){
//Create a cell with the no data image
tableView.scrollEnabled=NO;
}else{
//Create your default cell with data..
tableView.scrollEnabled=YES;
}
return nil;
}
I am just giving you an idea, its your choice of using it..

Resources