Refreshing a UITableView When Collection is Updated on Other View - ios

It is hard to describe my scenario but I will try. I have a UITableViewController which allows the user to see the details of an item. After the user visit the item details controller they open another controller and then add more items. I want to refresh my original UITableViewController once the items are added.
I can of course use the viewDidAppear event but I have noticed that it slows things. viewDidLoad is only fired once.
Any other ways to update the UITableView remotely!

You can use view will appear instead if view did appear.

Related

How to change UITabbarController's More ViewControllers UITableViewCells images?

More viewcontroller images are showing up in default blue background instead on original images. I want to change them to original images and also want to increase UITableViewCell's height.
I have tried implementing tableview delegate methods on UITabbarController, but nothing working out.
Even moreNavigationController is also not changing.
1) Where do i have to write these custom methods viewDidLoad or viewWillAppear. Also shouldSelectViewController is not getting called. First time when i select a tabbar item it is being called and when i select the more item it is not getting called.
2) Where should i implement UITableView delegate methods.
My screen -
required screen -
A solution using swift is helpful. Thanks in advance.
I got the requirement working. The step wise procedure I followed is
Take a ViewController which extends UITabbarController.
Take an array with five elements exactly.(If more than five then default More functionality gets enabled).
First four tabs as required and fifth should be 'More' tab. With required tab images.
For 'More' tab create a View Controller which extends UITableView delegates and datasources. And implement respective methods of delegate and datasources.
Take an array of the Cells you want to display and display them.
Link this 'More' View Controller to the 'More' tab.
Change the images in tableview in cellForRowAtIndexPath method.
Run the application and check by selecting 'More' tab. This time it should show the changed UI.
When each cell should show different View Controllers on selecting the cells, then segue to different view controllers using the segues from ViewController (Not segue from the tablecell). Define a unique segue id for each segue and show the viewcontrollers based on switch or ifelse conditions in shouldSelectViewController using performSegueWithIdentifier.
Thanks #KKRocks for the valuable links.
There's some content to be unpacked here. I am not entirely certain I fully understand the question but I'll go ahead and reply partially as best I can.
ViewDidLoad and ViewWillAppear
You add code in viewDidLoad for initialization. This method will run when your view is originally loaded and unless you're instantiating the controller every time it will only run once ( in this case you dont because you're using the prebuilt tabbar system which takes care of that for you)
So I would add the styling code in viewDidLoad()
When a viewController is to be displayed, viewWillAppear() will be called. This will happen every time so if you need to perform repetitive tasks like network calls, restyling based on some other data, refreshing your tables, you should do it here.
Table methods
For doing whatever you need to do with a table you will most likely need both UITableViewDelegate and UITableViewDataSource to your view controller. Look up any UITableView tutorial online they're many very comprehensive and will explain how to add all the table methods you need in your viewController.

How to edit a UICollectionView item in another UIViewController?

I'm using a Master Detail View Controller with Swift.
When I click on a master cell, I see a collection view containing a list of pictures.
Each picture represent data stored inside Core Data.
Each picture data has also a timestamp and all picture taken the same day are stored in the same Master cell.
[Date of the day 1]
[pic1]
[pic2]
[Date of the day 2]
[pic3]
[pic4]
What I want to achieve is:
When I click on a picture, another ViewController is displayed (through a segue) and this new VC allows me to edit the picture and change its date.
The thing is I have multiple things happening here when I change the day or a picture:
if the pic was alone in the collection, then I also have to remove the Master Day cell too.
if the pic was not alone, I just need to update the detail collection view
In any cases, if I have to create a new Day, I need to create a new Master Cell and update the view and if I need to delete a Day, I have to also update the Master view.
If you edit the collection items, you can simply do a:
self.collectionView performBatchUpdates:
and then update the model, and notify the collection view of your action...
but I don't see how I can do that from another UIViewController.
Currently, in the Edit View Controller, I update the model and then dismiss the View controller. And I get a crash because the collectionView tries to display an Item that is not part of the current collection anymore...
To solve this issue, maybe I can use a protocol in order to inform the detail view from the edit view but If I do that, I need to give the IndexPath of the Edit Cell to the edit view and then give it back to the detail view through the protocol... Why not. but not that clean in my opinion.
Another problem is, the app seems to also crash in the master view when I delete a Day. Because, the master view doesn't know that I deleted a day from the edit view.
My main problem is that I don't see how to tell my Master View Controller that I made some model changes... especially when I don't have any IndexPath information from within the edit view.
Technically, I could use the notificationCenter to notify both Views, I guess... but still, I would need to pass both IndexPath from the Master view to the Detail view and then to the Edit view.
Ugly.
Several ways by which this can be achieved:
1) Delegation: The view controller containing collection view will act as a delegate of Edit View Controller and when editing is done it will callback a method to 'The view controller containing collection view'
2) Notification: EditingFinished
3) blocks
Using protocol is good solution but can be painful since you have to manage edited items using their indexPaths. A simpler solution, since you are using CoreData, is to fetch entries using a computed property and just call collectionView.reloadData() on viewWillAppear method from the controller that contains collectionView. This way calling reload data will recompute Core Data entries and will place each item in correct position. Also viewWillAppear will get called after dismissing controller that stands on top of collection view controller
I found an interesting article addressing my problem.
It was not indeed a trivial problem...
http://www.objc.io/issues/4-core-data/full-core-data-application/#using-a-collection-view

How can I refresh view every time when a tab bar item is clicked?

I have an UITabBarController(empty) which have 2 subView. These two subView are tableView(get data from database). I want to refresh view every time when a tab bar item is clicked(get new data from DB).
I search many method on Internet. Some said use the viewwillappear. But did work.
Can anyone give me some Suggestions? Thanks
You set something to be the tab bar controller delegate and when the user clicks on a tab bar item, override:
tabBarController:didSelectViewController:
and each time the item is touched, send a message to the view controller (which is one of the parameters that comes in via that delegate method) to refresh whatever you want.
Don't use "viewWillAppear". That's meant to be called right before the view is meant to appear.
I'd suggest implementing a custom "refreshView:" method (or you can choose whatever method name that makes sense to you) and inside there, do whatever UI refreshing you want to do for the view owned by that view controller.

Executing Event on Unwind Segue

I'm not sure if I explained this correctly in my question, so I'll explain it here. Please excuse my iOS illiteracy, complete newbie here.
The master view controller is a UITableViewController that holds a list of items. So, each cell contains an item. This list is populated from a Core Data model.
If I want to add an item, there's an Add button that takes me to a different view controller that has a text field. I write the item name in the text field, press the Done button and it adds it to Core Data, and unwinds back to the UITableViewController.
I want the table view to reload its data as soon as the unwind happens, so the user sees that his item has been entered into the list. I have to use a refresh control in order for the table to reload its data.
Is there a method whose code runs once the part written in bold happens?
Thanks in advance.
You can reload your table view data in the viewWillAppear method of the UITableViewController, which is called every time your view appears on screen. You can see this question for more details on the view controller lifecycle.

Refresh split view

I have a universal application with a list of items (loaded from a backend) and item details (loaded from a backend as well). Each view controller listens to the UIApplicationDidBecomeActiveNotification, so that the view is refreshed when the user (re-)opens the app. That works so far.
Now my problem. On iPad, I have a split view. So, when the user (re-)opens the app in landscape, both views are reloaded. If there is no connection to the backend, the user gets two alerts with Retry/Cancel options, one above the other. That is not what I want... I have one default item which does not require connection to the backend and I want to set it to be selected and to display its details in the detail view. Always when the originally selected item is missing in the master view.
What I have done so far... In my master view controller, I check whether the selected item is available after the refresh and if not, I update the selection and the detail view. This should solve the problem when the requests from the left and from the right pane are processed in the correct order. However, currently both view controllers get the UIApplicationDidBecomeActiveNotification and make asynchronous requests to the backend.
Has someone experience in refreshing split views? What would be the right way to solve my problem? I really don't want to introduce additional notifications/complexity. I hope, there is some standard way to reload the data.
Well, I found a solution.
I create for each view controller a property alertView and store there the alert view that is displayed. In viewWillDisappear, I dismiss this alert view. So, when the details for my default item are displayed to the right, the alert view of the "old" controller is dismissed and I have only one alert view.
It is for sure not the perfect solution and I would be happy if someone can give me a better one. But for now, that works fine.

Resources