I am trying to learn iPhone development as I've read many others on here are also saying. And Ive searched through about 20 pages of previous threads but couldn't quite find one that is what I need. I have a TableViewController as my root view that is inside a NavigationController. I have a grouped table with 2 cells inside the TableViewController and was wanting the text of which ever cell was selected to become the title of the next view's NavigationBar. I am using Xcode 4.3 and storyboards. Any help would be appreciated!!
In the tableview:didSelectRowAtIndexPath: method, I assume that you are creating and pushing your next view controller. You also must be having an Array (or some data source) which populates the text in the Tableview. So get the current cell's text by using indexpath.row. Set this text to:
nextViewController.title = cellText;
So when you push this view controller, it'll have the title of the cell selected.
try below it in didSelectRowAtIndexPath method will help you
detailViewController.title = [[[self tableView:tableView cellForRowAtIndexPath:indexPath] textLabel] text];
Related
I currently have two UITableViewControllers, one with the prototype of the custom cell, and the other one not. Since both of them are connected through a push segue from the first to the second. I pass the selected custom cell to the second tableView.
Everything works fine, except that when I come back to the first view, the selected (and passed) cell is hidden, but when I scroll it enough to hide the blank space and scrolling back it shows.
How can I fix this?
The Big Nerd Ranch Guide says basically to stay away from Storyboard for just this kind of reason.
Amongst other things about Storyboards, it says:
"Overall storyboards make easy code easier and difficult code more difficult".
Not sure you should be passing a cell through the push segue.
When not using storyboard, you would typically create a new file of type UITableViewCell called e..g MyCustomerTableviewCell.
Then in your UITableView viewDidLoad method, create a new Nib and add it to the tableView,
UINib *nib = [UINib nibWithNibName:#"MyCustomTableViewCell" bundle:nil];
// register this nib that contains the cell
[[self tableView] registerNib:nib forCellReuseIdentifier:#"MyCustomTableViewTableViewCell"];
Then create a new empty XIB file (file->new->User Interface->empty) and call it MyCustomTableViewCell.xib.
Drop a UITableViewCell into the interface builder space, change it from UITableViewCell type to MyCustomTableViewCell, make sure it's File's Owner is MyCustomTableViewCell.
Then in your UITableView file, do this...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomTableViewTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:#"MyCustomTableViewCell"];
...
return cell;
}
You can probably do this as part of a storyboard project anyway (though I haven't tried missing XIB and Storyboard in one project).
If I understand correctly what you are doing, you are passing a reference of the tapped cell to the second view controller, and then you are showing it inside the table view of the second controller passing the same reference in the cellForRowAtIndexPath method.
It doesn't seems a good choice :-)
First: because that cell was allocated and queued for use in the first view controller. At any time, the cell could receive a prepareForReuse message by the first view controller.
Second: when you show the cell in the second table view, the cell is removed from the first table view.
If you don't want to create another prototype cell in the second view controller, you can, for example, allocate a new cell in the first one, copy all properties from the tapped one to the new, and then pass the new to the second viewcontroller.
This is the first solution I've thought about, but sure there are other better solutions :-)
I have a page with a button on it. When the button is pressed, i want the table view on the next page to add a cell with a label that has the text of 1 and i want there to be a picture also, but i know how to do this. I just want to add a cell to a table view when a button is pressed on the page before the table. How can i do this? Ive tried this code:
FinalCartViewController * viewController = (FinalCartViewController
*)self.parentViewController;
custom.customCellLabel.text = #"1";
[viewController addObject:[NSMutableDictionary dictionaryWithObject:
custom.customCellLabel.text forKey:#"name"]];
custom is an ivar that is created from the FinalCartViewController.
This code was in the method for the button that i want to use to add the cell. And i used [myTalbleView reloadData]; in the tableView's viewDidLoad. But my app crashes when i press the button to add the cell. Could somebody please help me? Thanks.
FinalCartViewController * viewController = (FinalCartViewController
*)self.parentViewController;
If the FinalCartViewController (I assume is the Table View) is the NEXT page, how can it be the PARENT view? The Parent view refers to the previous view.
You need to initialise the FinalCartViewController and save reference to it, so you can add objects to it. Although, the better practice would be to have a shared datasource that you add to and then pass to the FinalCartViewController.
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.
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.
I have a UITableViewController, in which I added a UISearchBar as the tableHeaderView using Interface Builder. Then I added a UISearchDisplayController in the nib, and set up all the connections (delegate, searchResultsDelegate, searchContentsController, searchResultsDataSource, all connected to the UITableViewController).
I then implemented all the delegate and data source methods in my code.
It works like a charm, except for a weird bug: sometimes the search results table view won't scroll, and I can see the flash indicator of the main table view behind it. I NSLog'd the searchResultsTableView and apparently it's a subview of the main tableview, and I guess that's the reason behind the touch problems I described earlier.
What's my mistake? Is it possible to use a UITableViewController with UISearchDisplayController at all? If so, how do I set up it in such a way that the results table view doesn't get added as a subview of my main table view?
Update: I found this sample which uses UISearchDisplayController with UITableViewController and apparently the search table view gets added to the main table view in there as well. So now I don't think that's my problem.
The thing is that I can't find any substantial difference between what I'm doing and what that sample is doing. I'm just adding an UISearchBar as a UITableView header in a UITableViewController and adding a UISearchDisplayController to it... It's like iOS is confused between the main table and the search table when I try to scroll. Do you have any ideas?
Update: Added a 200 rep bounty. Please answer only if you know what you're talking about.
It was an extra self.tableView.scrollEnabled = YES which was called as soon as the search data request was finished. Hope it helps anyone with the same problem in the future.
A search display controller manages display of a search bar and a table view that displays the results of a search of data managed by another view controller.
Here the searchDisplaycontroller combines the searchbar and tabelview which showing the result data in the tableview so it won't required separate tableview.
While when you using UISerarchBar then it need to have atleast one UITableView, which can show results in the view. And here you can see the result without even adding the SearchDisplayController to view.
I think you might want to try adding a UISearchDisplayController IBOutlet to your class and connect that to your SearchdDisplayController in the nib file. I am amusing that that searchdisplay controller in your nib automatically connected it self to the default serachdisaplaycontreoller outlet in the inspector.
The search bar controller uses its own table view that is added on top of your own. If you set the delegate and dataSource of the UISearchDisplayController to your UITableViewController, then the table view controller has to make a distinction between the two table views in its delegate methods. If you don't do that properly, this may be causing your bug.
In other words:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.tableView) {
return number of rows in data model;
} else {
perform the search query;
return number of rows in search query;
}
}
You do something similar for all of the delegate and dataSource methods.
I'm not 100% sure that this is what's causing your problem, but without seeing more source it's hard to say.