Update Table View on command from View Controller - ios

New to iOS programing and objective-c and am getting stuck on something which I am guessing is simple. Here's my question:
I have a simple, single view application with two text fields, a button and a table view. The idea is that I want to take the text from the two text fields and populate the next available cell in the tableview with this text. I've got everything sorted out except for how to update the contents of the table view on command. Ideally, I'd like the table view to be updated upon pressing the button.
I have a View Controller which is serving as the data source and delegate for the table view.
I am aware of the [tableView reloadData] method, but it requires the appropriate tableView variable within scope. This is fine in say a delegate method like tableView:didSelectRowAtIndexPath, and I've gotten it update upon selection of any cell, but what if I want to update the table view when an action unrelated to the table view is performed, say when the button is pressed? There is no tableView variable in scope of this method to call reloadData from (or is there)?
So I guess my question is: is there a way to access the table view variable from the rest of the view controller for the purpose of calling reloadData OR is there a method I'm not aware of that would facilitate this?
Thanks for any help!

Why do you you create an outlet for your table view in view controller?
Your view controller will be delegate and data source for the table view (you should set in interface builder or from code:
self.tableView.delegate = self;
self.tableView.dataSoure = self;
I am assuming that self.tableView - is the outlet that I mentioned. When ou have an outlet you can easily update table view after pressing the button: e.g you add new value to an array that is used for filling table view and call
[self.tableView reloadData];
If you do not want to reload all table you can use method insertRowsAtIndexPaths:withRowAnimation:
Actually you have to read next manuals as well:
https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/tableview_iphone/AboutTableViewsiPhone/AboutTableViewsiPhone.html
This references are really useful.

Related

Using the same table view cell in multiple view controllers

I have a custom table view cell class, which I use in several view controller's table views. I am adding a feature that gives the user the ability to delete a table view cell and I'd like to remove the cell from the table view when they do so.
Using a delegate is one way to achieve this, so that when the user deletes the table view cell, the table view cell class informs the view controller, through the delegate, that it was deleted and to remove it from the table view. However, this would mean I'd have to do this for each of my several view controllers, which I don't want to do.
So my question is, how can I implement this in such a way that I only have to write the code to remove the table view cell from the table view once?
Subclass the UITableView. Create a protocol like:
protocol customTableViewDelegate {
func deleteTableViewCell()
}
Then give the subclassed UITableView a variable of this type:
var removeCellDelegate: customTableViewDelegate?
And then instead of adding a normal UITableView to your view controller, add the custom one and set the removeCellDelegate to the view controller. Then implement the function to do whatever you want.

UITableView reloads on show view controller

Please help me to understand why table view reloads on view controller start. I setup outlet for table via storyboard and data source, delegate. I did't call tableView reloadData method.
So When view controller starts '..numberOfRowsInSection' and other table view delegates methods called.
as Apple docs say:
UITableView overrides the layoutSubviews method of UIView so that it
calls reloadData only when you create a new instance of UITableView or
when you assign a new data source. Reloading the table view clears
current state, including the current selection. However, if you
explicitly call reloadData, it clears this state and any subsequent
direct or indirect call to layoutSubviews does not trigger a reload.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/
If you really do not need its reloading, create your tableView as a lazy variable and add it to a view programmaticaly when you need it.

Common pattern with segues and table views in iOS

I have the set up of a user clicking a UITableViewCell, this triggers a segue to a 'detail' view controller which is popped onto the stack.
First,
User Taps -> [myVC tableView didSelectRow...] - this is where I can work out which cell and therefore which model object my user wants to modify or access.
Second,
User Taps -> [myVC prepareForSegue...] - this is where I set up my detail view controller with the correct model object.
So, do I just store the selected model object in an instance variable between the two functions being called?
In prepareForSegue, the sender argument will be the cell, so you can work out which one was tapped at that point. You could also check the tableview's selectedIndexPath property.
You don't need to implement both functions.
I understand that you need to pass an instance of your model to your details view controller after you tap on a table view row. You have some ways to achieve that:
You can create a property in your view controller with a data type of your model. Let's name it for example firstObject. Then, inside didSelectRow you can set this property or update it according to your needs. Then, inside prepareForSegue you can pass this instance to your details view controller, by doing something like: detailsVC.detailsObject = firstObject.
Inside prepareForSegue, you can get the value of the current tapped table view row using indexPathForSelectedRow.row. Assuming that you have an array holding different objects of your model that you need them to be sent to your details view controller based on the tapped table view row index, you can do something like:
detailsVC.detailsObject = myObjectsArray[myTableView.indexPathForSelectedRow.row].
I hope that I was able to make it more clear for you.

Populating a Text Field with a Selected Cell from a Separate Table View Controller

I have a working app which just needs some modification. It's a basic app with a Table View Controller that gets populated when a user taps the Plus button and fills in some information into text fields. The user will enter a name and event and I've made life easier by creating table view cells under the text fields so that when a user starts typing, it auto populates with the same names the user has entered before.
I'm using Core Data and NSFetchedResultsController. I'm modifying the app to be more seamless now for the user. Instead of the keyboard popping up when selecting the text field, I'm taking the user modally to another Table View Controller where the user can search, create or just select an existing entry from the Table View Cells.
I've got the new table view controller appearing and displaying the existing entries using NSFetchedResultsController and that's working well.
My question is: how do I go about selecting a Cell in the new Table View Controller and having that selection of the cell do two things:
1) Dismiss the Modal View
2) Populate the name text field (in the view controller that brought up the new Table View) with the selected name from the table view controller (that came up modally).
I have this working if the table view is in the same view controller, but I'm not quite sure how I would go about extracting that information from the other table view controller.
I'm guessing I would use protocols but I'm quite a newbie and so any pointing in the right direction or even some simple sample code would be massively appreciated!
Thanks,
You have to implement delegate protocol.
The idea is VC1 should conform the delegate (i.e. need to have a procedure -(void)dataAvail:(NSString *)data withViewController:(ViewController *)sender{...}). VC1 will push VC2 and set VC2.delegate = self.
In VC2, when cell is selected, just call [self.delegate dataAvail:self.yourTextField.text withViewController:self];
Now, in your VC1, you should implement dataAvail... just get the value and dismiss your modal VC2.
For more info on how to declare the protocol, just google for "ios delegate tutorial". delegate Protocol is used everywhere when coding for iOS (like MVC or KVC).

referencing UITableView to another tableview on a split view based app

I'd like to understand what's going on here. Basically I have a NumberOneViewController which owns a table view and is showing up on the details view controller on a split view based app.
When a user selects an entry on the NumberOneViewController tableview row, i assign the table view to RootViewController's tableView member like so:
self.tableView = numberOneViewController.tableView;
So the table view on the detail view controller is now gone - which brings me to my first question, what exactly happened here?
Now, I want number one view controller's table view again to show up on the details view controller, but how?
Maybe you need to reload/refresh the table view after you assign it. Just throwing out something off the top off my head since there are no other answers. Good Luck.

Resources