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
Related
I'm using UITableViewController in order to use static cells.
Is there any way to freeze first cell so it's not scrollable? I'd like to other cells to scroll but first one to stay intact.
I was thinking about regular UIViewController with some stuff on top and UITableView under, but this won't work because I need UITableViewController in order for static cells to work.
The only other way to make it work is with UIViews and Scroll view, but then I don't get benefit of UITableView.
Is that possible?
EDIT:
Just to make it clear. I like to use UITableView with static cells because I have outlets and actions. Buttons, fields inside cells. I'd like to limit "coding" only to show/hide cells. I don't want to create UI inside code. I want to do as much as I can using Storyboard.
Not sure how to put views on section header using Storyboard. Is it possible at all? I only can set text caption.
Also! Even though I do have header (with test) it still scrolls with rest of cells. I need to make sure it stays on a screen.
You can use this delegate method and pass your custom view along with your sub control it will get stick at top :
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return <your custom view>;
}
I am new in iOS programming and I would like to create I view for the settings of my app that looks like iPhone's one (like in the picture). Some of rows will call other views when taped and other will not.
What is the best way to do that? I thought about TableView with prototype cells, but is there a best way to do it? or a best-practice for what I want to do? Or maybe a tutorial online?
The fast way in Interface Builder:
Use a UITableViewController, make it STATIC and use the GROUPED style (all in IB).
You can setup the cells to show disclosure indicators (or not) in IB also.
You can segue directly from the rows or the UITableViewController to where you want to go.
If you segue from the UITableViewController, implement the "didSelectRowForIndexPath" method and call "performSegueWithIdentifier" accordingly.
A structure like this is best by UITableView.
First you select how many sections you want, and customize each section with a data structure that you have to be filled with (Probably an array.)
Then you fill up each rows inside
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method, and call your value from the array/dictionary that you have.
for going to a next view when clicked upon
Use the method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Hope this helps
The best way to do that is using static UITableViewCell.
See UITableView Programming.
The optimal solution here is undoubtedly UITableView. This is because firstly, you have the need to display a list of options that would have external links to other pages and UITableView is designed and used for this purpose.
In an addition to that, if you want, you can also expand and collapse the rows of your parent TableView into a Child TableView i.e a UITableView as a subview of its parent UITableView.
Put up a UITableView and populate it with UITableViewCell. That will be just fine with the requirement you have.
Hope this helps.
I am trying to set up a UITableView inside of a UIViewController. I'm doing this because it allows me to add a top bar with save and cancel buttons. I'm using storyboard and static cells to model the tableview to get input from the user (think of the create new event in Apple's calendar app). I have the view in Xcode, but when running it on my phone or the simulator, the tableview does not display. Here is the simple view in Xcode:
And this is how it displays when running it:
I've read about adding delegates and setting the datasource and such, but really this is all just going to be static cells with text fields, no data being loaded. Why is this happening and what can be done to fix it? Thanks!
#Made2k It looks like you found a solution, but for others who come across this with the same issue, note that you can only use a UITableView with static cells inside of a UITableViewController. This is not a well-documented fact, but apparently only UITableViewController knows how to handle the layout of static cells, whereas a regular UIViewController just needs to implement the UITableViewDelegate and UITableViewDataSource protocols in order to handle display of content in a UITableView added either programmatically or via Storyboard/Nib.
Hopefully this will save someone some time. #Made2k's solution is fine, but does make your storyboard unnecessarily busy. If you don't need to use a UIViewController, then just do your work inside a regular UITableViewController and save yourself a headache.
If you want to use a UITableView in a UIViewController you have to make the ViewController a data source and a delegate of the TableView and then implement methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
In case of a static table, in the cellForRowAtIndexPath you'd return outlets of the static cells. For a detailed description check out this answer:
https://stackoverflow.com/a/19110821/3110536
I ended up thinking about this in a different kind of way. The problem I thought I was having was I wanted all the features that a navigation controller provides, but I needed this to be the base of the controller, i.e. nothing to go back to. I was thinking that the only way to do this was to create a UIViewController and add the table view and such in there, but what I came up with is to simply just create a new navigation controller and now this view shows up as the root view like so:
I don't know if this is the best practice, but hopefully it can help somebody else if they are having this problem.
Man, following Hack really works!
You should give it a try!
In my requirement I wanted to add buttons in my Static cells too!and Toggle the visibility of the TableView
[self.tableView setHidden:YES/NO];
and Reload it with new data
[self.tableView reloadData];
and so many things is possible with that way of doing it!
https://stackoverflow.com/a/19110821/1752988
Hope the above link would help you! (Y)
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.
I would like to implement a form to insert data within a TableView in my app.
I would like the form appears as a table, and each row of the table represent a TextField.
What is the right approach to do this?
Any help is appreciated!!
Regards, yassa
If I understood your question right then I would go for custom TableViewCells.
Subclass UITableViewCell.
In the init method (initWithStyle, if I am not much mistaken) create the Text Fields and add them to the superview. ([self.view addSubView:myTextField])
In the layoutViewItems method you should arrange the TextField and any other custom view that you may want to create and add in init.
While overwriting both of them, do not forget to call the related superclass method at the very beginning. You may not need [super layoutSubviews] in the event that you do not want to use any of the standard elements of UITableViewCell.
If you are using Storyboard, use the new Static Cell TableView (as opposed to Dynamic Prototypes. This allows for a TableView that you don't have to use the delegate or datasource methods on. You can layout the sections and rows manually in IB. I use it lots for form based views.