iPhone - multiple tables in one view - ios

I'm looking for guidances on building interface for my test application. I'm new to iPhone development so I'm not sure how should I approach it.
I want to display elements in multiple tables in one view. It should be possible to change table using swipe gesture and drag it's elements between each of tables. I plan to add some cool animation for dragging and adding new elements to tables. Should I use 'drawing 2d graphic' for that purpose or is there other way to do it (reuse tableview). Does anyone have any examples how to do it?

It would be complicated but a way to go...
Keep one datasource & delegate.This means that all the delegate/datasource methods become more complicated BUT it means that you can retain the one to one relationship between viewController & view.
keep a reference to each of the table views
//vc.h
#property (nonatomic, weak) IBOutlet UITableView* firstTableView;
#property (nonatomic, weak) IBOutlet UITableView* secondTableView;
In the datasource/ delegate methods you need to account for the fact that the method needs to behave differently depending on which table view is in use. e.g.
//vc.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
if (tableView == self.firstTableView) {
...
} else { // tableView == self.secondTableView
...
}
}
return cell;
}

Use two UITableViews in one UIViewController. Have each of their delegates and datasources in different model classes.
To drag things between them, you will need to create a table view cell view when you start holding on a table view cell so that when it is duplicated, you can move it around using a UIGestureRecognizer. Once you have that, to drop it into another UITableView, you'll need to check for its position and drop it in. Remember to update your model.
This is not very easy to do as you'll need to do many positioning and math stuff, but I'm sure you'll be able to figure it out.

Related

Is it possible fix the position of first row on UITableController?

In my app I have used a UITableViewControllersub class to display a list of data. I finished every thing in the project, but the client needs the top row of the table to stay on top and not scroll with the rest of the content. I know this could easily be achieved using a UIViewController instead of a UITableViewController but would like to avoid this. I'm here for a final attempt to see if there is any way to fix some rows in a table view.
Please note that there are 4 different UITableViewController's in my project and they all have some complex logic in their table view delegate methods. This means a lot of work if I need to change all the UITableViewControllers into UIViewControllers.
Anyone have any ideas?
I have two suggestions for you:
More common practice is to use property of the UITableView
#property(nonatomic, retain) UIView *tableHeaderView
Second way to place your table view as child view and add another subview
UIView
HEADER UIView
UITableView
All you need to do is to implement method (declared in UITableViewDelegate):
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return self.yourHeaderTopView;
}
You need to create the custom tableheaderview. For more refer this
Add 2 cells
use first as static and fill up the information as u want.
set the Reuse modifier of the second and call that second cell in the
cellAtRowForIndexPath method to use that second cell continuously

Why can't I change the custom class of my UITableView?

Apologies if this question is a little basic but I've spent several days trying to understand the root cause of this problem without any success.
I am working on an app which relies heavily on UITableView objects. I can successfully use a UITableViewController object and display information in a table but I need to be able to have multiple tables on screen citing data from multiple sources and the UITableViewController seems to be too limited.
I would like to be able to place multiple UITableView objects with the storyboard then create custom class files which manage the tables. Unfortunately when I've tried this, XCODE doesn't let me select these custom classes to manage the tables.
Although I've found some potential workarounds online I want to understand why selecting a new class to govern a table view is not possible.
[I wanted to post images but apparently I can´t until I have a better reputation...]
It depends on exactly what you did. But, you should really take a different approach:
If you can, use a single table view with multiple sections (with headers / footers).
If you can't do that, create a separate table view controller and table view for each section of information that you want. Then, your 'main' view controller should act as the parent and add all of the other table view controllers as children (addChildViewController:) and their views as subviews. This approach will keep your code segregated and organised rather than trying to have one controller manage many disparate views.
.h
{
UITableView *objlefttableview;
UITableView *objrighttableview;
}
.m
viewdidload
{
if(!objlefttableview)
objlefttableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 87, 227, 681) style:UITableViewStylePlain];
if(!objrighttableview)
objrighttableview=[[UITableView alloc]initWithFrame:CGRectMake(227, 87, 263, 681) style:UITableViewStylePlain];
[objlefttableview registerNib:[UINib nibWithNibName:#"View" bundle:Nil] forCellReuseIdentifier:#"leftCell"];
[objrighttableview registerNib:[UINib nibWithNibName:#"ViewR" bundle:Nil] forCellReuseIdentifier:#"rightCell"];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self->objlefttableview==tableView)
{
}
else
{
}
}
so what i just did is i created two tableview objects and then i gave two different custom cell to both of them
If u need more help in this approach do ask
The problem was that where as the UITableViewController object is by default the UITableView delegate and it's datasource, a UIView is not even if it inherits from UITableViewController. I hadn’t specified that and it seems that neither 1 nor multiple tables could function as they had no class governing them set to be delegate and data source.
By specifying in the ViewController’s .h file that it was also the delegate and datasource for the UITableView like below (the delegate and datasource commands should be surrounded by triangle brackets but they aren't displayed on this for some reason):
#interface DHViewController : UIViewController [UITableViewDataSource, UITableViewDelegate]
and in the .m file’s viewDidLoad method specify that it was the data source and delegate for both tableViews like so:
self.tableAnswers.delegate =self;
self.tableAnswers.dataSource = self;
self.tableQuestions.delegate =self;
self.tableQuestions.dataSource = self;
and implementing the necessary methods in the .m file:
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
…both tables can be independently managed and displayed on the same screen.
Thanks a lot to all for your help!
FYI (I think I will still go for #Wain ’s idea of having a single table with section specific content/behaviour - it seems much neater).
1) Add UITableView for your storyboard.
2) Set delegate and data source.
3) Create Outlets (properties in your class) for those tableView's
Then you can work with thouse table views. For example, place label on it, and change it text dynamically in your programm.
Cheers :)

Adding a number picker to each table view cell

I am very new to iOS development. Currently, I am trying to develop an iPad app for taking orders in a restaurant. The UI should be like this:http://www.dhtmlx.com/blog/wp-content/uploads/2011/02/ipad_menu_final.jpg As people suggested, it would be easier to start by creating a Master/Detail project as the basic structure. After going through a couple of tutorials, I know how to display detail contents when I select rows at master view. Now, my assumption is to add an UITableView to the detail view controller, and at each UITableView Cell, I want to add some text to describe the menu item and also an UIPickerView to let the user select the quantity of each item (like the pic in the above link). Am I on the right track? How can I add an UITextField and UIPickerView to each UITableView cell programmatically? Are there any examples or tutorials teach me how to do this?
Thanks in advance.
1 - THE VIEW : Create a UITableViewCell subclass (ex: MyMenuItemCell), with the properties you need
a UIImageView for the picture
a UILabel for the price (with some CALayer cornerRadius, backgroundColor, and borderColor tweaking)
another UILabel for the title describing the product
another UILabel for the description
a UIButton to get userAction, but handle that in another class, as this cell is the VIEW, and should hold no control logic, this is the CONTROLLER's part.
2 - THE CONTROLLER
You UIViewController should implement <UITableViewDatasource, UITableViewDelegate>. (or it can directly be a UITableViewController subclass, which provides some generic benefits, like moving the tableView when a keyboard is displayed. If you choose this 2nd option, and create the viewController programmatically, be sure to use the initWithStyle: method)
3 - BIND VIEW with CONTROLLER
However you choose to enable 'quantity pick' :
with some UIStepper (+ and - buttons) linked to a UILabel
or
in some dedicated UIPickerView displayed in a UIViewController embedded in a UIPopoverController
(or any other design you see fit)
you should write the code that triggers this action handling IN your controller, when UITableViewCell are instantiated / reused, in UITableViewDatasource method providing cells instances :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

2 UITableViews in one UIView

I have a UIView that will need to display two UITableViews, but they are never shown together, by using a SegementedBar you can toggle one or the other.
What would be the best way to handle this? Just create one Table View Controller and change the data source, or create 2 Table View Controllers and just hide one when the other is visible.
The 2 tables will have a completely different layout with different custom cells.
I would keep one datasource & delegate.
This means that all the delegate/datasource methods become more complicated BUT it means that you can retain the one to one relationship between viewController & view.
keep a reference to each of the table views
//vc.h
#property (nonatomic, weak) IBOutlet UITableView* firstTableView;
#property (nonatomic, weak) IBOutlet UITableView* secondTableView;
In the datasource/ delegate methods you need to account for the fact that the method needs to behave differently depending on which table view is in use. e.g.
//vc.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
if (tableView == self.firstTableView) {
...
} else { // tableView == self.secondTableView
...
}
}
return cell;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
if (tableView.tag == 1) {
...
} else { // tableView == self.secondTableView
...
}
}
tag could be assigned from the .xib.
so no need to have UITableVeiw variable in .h file.
Two table view in .xib needed
Both approach has some pros and cons, but i will personally prefer approach having two separate controller.
Approach 1 - create one Table View Controller and change the data source
This approach help in avoiding extra and repeated code.
With this memory management is good as using one controller only.(Although this is not a big concern till then we won't have a lot of data.)
Issue with this is having complexity.
Approach 2 - 2 Table View Controller
With this approach definitely have extra and repeated code.
But with this is less complexity.
In my current app, I need to have 4 UITableView in a single UIViewController, at once I've to show single table, based on the tab selected by the user, I've added four tables because, all of having different custom cells and functionality, to reduce complexity I took four.
The main benefit of this is that, each time you don't need to call reloadData to update a single table. I just need to properly handle table's show & hide flow. And believe me that's looks cool. Not flicking at all.
In my case, I am creating four tables by code only. And I make a method that will return me a table based upon a tag I've pass.
I keep cellForRowAtIndexPath as small as possible by dividing code into different functions.
Use separate UITableViewControllers and swap the views. It's less code, less complexity and it's the way Apple does it with the TabBar.
As for code complexity, there really isn't any. You simply do the following to switch views when the UISegmentedControl's value has changed:
UIView *previousSuperview = myViewController1.view.superview;
myViewController2.view.frame = myViewController1.view.frame;
[myViewController1.view removeFromSuperview];
[previousSuperview addSubview:myViewController2.view];
Alternatively, you could set the corresponding view's hidden property.

Use UISearchDisplayController as a superclass

I've put a lot of effort into creating a solid UITableViewController with custom cells. Now I want to create a separate UITabbarItem that uses that UITableViewController within a UISearchDisplayController.
Adhering to OO design principles, I imagine that when defining the UISearchDisplayController I'd subclass the original UITableViewController.
e.g.
#interface SearchViewController : CustomTableViewController
{
NSArray *listContent; // The master content.
NSMutableArray *filteredListContent; // The content filtered as a result of a search.
// The saved state of the search UI if a memory warning removed the view.
NSString *savedSearchTerm;
NSInteger savedScopeButtonIndex;
BOOL searchWasActive;
}
However this approach doesn't work at all - the cells are not updated at all in SearchViewController, and the UITableView delegate methods do not seem to have an effect (e.g. rows are not resized).
So I have several questions:
Is this the correct way to go about this, if so, how do I update the listContent and the filteredListContent from the superview.
Would it be better to just add a UISearchBar to the original search view and hide it as necessary?
I don't think you can subclass UISearchDisplayController and have it work correctly. It does a lot of stuff in methods which aren't public, so you wouldn't be able to override them with the correct behavior.
You can, however, use the built in UISearchDisplayController with your custom table cells in the search results, as is. You need to encapsulate the creation and configuring of your custom cells such that it works in any table view just by overriding -...cellForRowAtIndexPath (this is the standard method of displaying custom data in a tableview). Make sure that controller is the UISearchDisplayDelegate and it'll use that method to create the rows in your search list.
To set the custom height, implement
- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)searchTableView
to set the rowHeight on the searchTableView.

Resources