Can we use initWithNibName to reuse same .xib file with different views? - iphone

I have a UITableView which contains 8 items (static). Now when i click each of 8 cells then each cell contains 1 single control.
For example:
Click cell 1: New view with UIPickerView.
Click cell 2: Another view with UIDatePicker
Click cell 3: One more view with UITextField.
.
... so on.
Can I use only one .xib file which contains all the controls but when i click that cell only that control is visible and all other are hidden.
Also i want that those controls value should be return to detailText of Table Cell.
How can I reuse one .xib file?? can i invoke xib file with different nib names and check in initwithnibname method??
please can u give me good example for this....

Basically the only way I find is to set conditional statements where in you show only some of the controls in a particular condition and hide the others and in other conditions similarly.
I think this is the only way you can show different controls based on cell selection in same XIB.
Hope this helps you.
EDIT:
You can use a flag (declared in application delegate) and set the flag in didSelectRowAtIndexPath method based on the cell selected.
Now on the viewDidLoad of the PickerView, you can check the same flag which would let you know which cell was clicked.
Hope this helps you.
EDIT-1
You can just use the NSUserDefaults as shown..
For Cell-1
[[NSUserDefaults standardUserDefaults] setValue:#"Cell-1" forKey:#"CELLSELECTED"];
For Cell-2:
[[NSUserDefaults standardUserDefaults] setValue:#"Cell-2" forKey:#"CELLSELECTED"];
For Cell-3:
[[NSUserDefaults standardUserDefaults] setValue:#"Cell-3" forKey:#"CELLSELECTED"];
and so on..
Hope this helps you now

I had this brilliant idea of using generic nib files on a Prefs screen, so I had a "switch nib", a "text field nib", and a "text nib". I assigned the controls to view controller properties programmatically in the cellForRowAtIndexPath. However, this ended up not functioning for some mysterious reason.
I ended up designing a custom nib for each cell. It works. Then, in the cellForRowAtIndexPath I have to determine which cell was loaded, to initialize the control to the proper value.

Related

Swift Add custom cell on button tap

I have a table view with one custom cell that I have already built and hooked up. When the user taps the add cell button I want it to add a different custom cell. I don't know where to start this process. Do I build it on the storyboard then hide it? Or what is the best means?
You should create all of your custom cells in the storyboard, and then implement UITableViewDataSource methods, where you return whatever cells you want.
Check out the documentation

IOS create new UITableViewCell with properties of a prototype cell

I am working on an app that has several prototype cells in one view. This worked well for easily altering the appearance of the app while in development using the storyboard. However, now I'm adding search (filtering) capability. I would like the appearance of the tableview to remain unchanged, just filter out some of the results.
My understanding is that I have to create new cells to do this. Is this correct? If it is, is there a way to create a cell with all the properties of my prototype cells. As it is now, the newly created (search result) cells have default settings.
Thanks.
You can certainly use copy and paste. Create a xib file (an empty one), and copy the cell you want from your table view in the storyboard, and then paste it into the xib file. In the viewDidLoad method for your table data source, register that nib file:
[self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:#"SearchCell" bundle:nil] forCellReuseIdentifier:#"SearchCell"];
Then in the cellForRowAtIndexPath method, you just dequeue a cell with that identifier for your search results table view.
The thing to understand clearly is that the table view that appears when you are doing a search with the UISearchDisplayController conglomerate is not your table view. It is a different table view, and you do not have a UITableViewController managing it - the UISearchDisplayController does that. Thus you must take other measures if you want that different table view to look like your table view.
EDIT: On the whole (and after the little exchange with rdelmar in the comments on his answer), I tend to think the easiest solution is to abandon the use of cell prototypes altogether. If you design the cell in a nib (xib), you can then use that cell both for the real table and for the search results table. In both cases you register the nib with the respective table view - and then dequeue just does the right thing all by itself, in both cases, with no change in the code.
You can see me doing something similar here:
https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/ch21p632searchableTable/p536p550searchableTable/RootViewController.m
... except that in that case I'm registering the same cell class for both tables, not the same nib. But it all comes down to the same thing. However, note that I do not start with a storyboard, so I never fell into the trap of using a prototype cell in the first place.

How do I maintain the UITableView selected row when returning from a UINavigationController child screen?

I have a UITableView of customers where a row/customer is selected. The user can then push another view on the UINavigationController stack to add a new customer. When I pop the child screen and return to the UITableView the previously selected row is no longer selected.
I can re-select the row in viewDidAppear() but it looks bad as you can see the deselect and the select. Is there a way to maintain the selected row when returning from the child screen?
I assume you are using a table view controller, as otherwise it is your responsibility to write this behaviour anyway. In a table view controller, though, it's easy. Just add in viewDidLoad:
self.clearsSelectionOnViewWillAppear = NO;
That will keep the row selected, unless you manually deselect the row or the user selects another row on the table.
If you select it in viewWillAppear:, it should get selected without the user seeing it. Would that solve your problem?
You need to call:
[tableView deselectRowAtIndexPath:indexPath animated:YES];
This is found in the didSelectRowAtIndexPath delegate method for a UITableView.
By default, a selected row gets deselected (with animation) when you return to that VC.
So, if you push a DetailViewController, the cell stays selected until you return to the RootViewController and then gets deselected automatically.
I used to have such a problem too. Make sure to always call the superclass's implementation of methods such as viewWillAppear, viewWillDisappear etc.
Also, do not call deselectRowAtIndexPath: manually.
This is possible :
Only in once case: If you have the static content in TAbleView,
just reload the TableView in ViewDidLoad only, not in View will Appear.
whereas make sure, you don't allow table to reload anywhere in same class in any method. Then you can achieve what you want.
The cell get deselect because of reload the TAbleView.
But for Dynamic content, you need to use the delegate method:
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
In this you need to pass the indexPath which you want to show selected with the scrollPostion same as tableviewposition.
You cannot have a tableViewCell selected all the time. It gets back to normal once it serves its purpose. I think you meant highlighted or appear to be selected, and I am answering the question based on that.
Get the row that was selected at the cellForRowAtIndexPath. When you pop your navigation controller and come back to the tableView, go the the cellForRowAtIndexPath and have that cell highlighted (You can use any UIColor as you wish) - there is no specific way to highlight stuff - use your imagination - Default highlight style is Blue, while the text is White.
How is this done ?
Find selected row in cellForRowAtIndexPath
Store is in an external variable / or NSUserDefaults and you can also use NSUserDefaultsto communicate between screen to find out if item was popped from that screen.
[tableView reloadData] in viewWillAppear
Check for NSUserDefaults value in cellForRowAtIndexPath and modify that cell with cell.backgroundColor or something.
Without some code, I can just give an advice : stop using [tableView reloadData], use [tableView beginUpdates], [tableView endUpdates] and – insertRowsAtIndexPaths:withRowAnimation: and – reloadRowsAtIndexPaths:withRowAnimation:. Things should work as expected.

NIB File Doesn't Display at Runtime

I am pulling my hair out on this one. I have a NavigationController with two levels of TableViews. Each TableView is in its own NIB file. The first level simply displays a list. Upon selecting a cell, it takes the user to a second level TableView with a more detailed list. It is on this second level TableView that I want to display a search bar (actually I am using a SearchDisplayController as well). I have added it to the TableView because I want the SearchBar to scroll with the table.
Below, I am displaying two screenshots. The first is the second level tableview in InterfaceBuidler. The second is the second level tableview at runtime. For some reason, the SearchBar doesn't display at runtime.
I have tried creating a completely new project from scratch and the same things happens. I don't understand why the SearchBar doesn't display on a NIB pushed on the NavigationController.
Before you ask, if I put the SearchBar on the first level TableView, it shows up just fine. Yes, I am adding it to the TableView itself, so it is a part of the view that should be displayed.
Help! What am I doing wrong?
This is what actually displays after the XIB is pushed...
Okay, I'm going to have to answer my own question. I thought about deleting it, but perhaps this could help someone else. I really pulled my hair out on this one. It wasn't because of anything I was doing wrong so much as a fundamental misunderstanding of how iPhone development works.
In a nutshell, the problem was with this line of code, which instantiated the TableViewController:
self.downloadDetailViewController = [[DownloadDetailTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
When you use XCode to create a new class, you can use the following:
"Add -> New File -> Cocoa Touch Class -> UIViewController subclass -> UITableViewController
subclass AND With XIB for user interface"
The problem is that the NIB has absolutely nothing to do with the UITableViewController until you tell your code to use it. To me, this seems like a bug in XCode or at the very least something that is counterintuitive. When the development environment creates all three files together, it would only make sense that they would work together, but they don't.
Instead, the solution is to modify the line of code as follows:
self.downloadDetailViewController = [[DownloadDetailTableViewController alloc] initWithNibName:#"SecondaryView" bundle:[NSBundle mainBundle]];
Hopefully this can help someone else...
You could also set Nib Name property on your Download View Controller under the Tab Bar Controller, in fact that and coupled with setting the class with your class name will automatically create the view for you and you don't have to manually create it.
I think the problem is that you are adding the searchbar to the tableView, so it is not displaying.
Try to add that searchBar with out adding to the table view, that means move the table view some what down and add the search bar to the view(not the tableview).

Customizing UITabBarController

I need to change font size and background color of the list displayed by "More" button of the UITabBarController. Is it possible ? How can I do it ?
Thanks a lot.
UITabBarController has a property called moreNavigationController, the root view of which is presumably the UITableView you see when you tap the "More" button.
If you want to customize the table view cells, you'll need to reassign its dataSource to an object you control. But, you'll need to implement every method of UITableViewDataSource and forward those messages to the original data source.
In your implementation of tableView:cellForRowAtIndexPath:, you'll be able to customize the cell returned by the original data source's implementation of that method.
Sounds like a lot of work just to change some fonts, doesn't it?