Tableview after tableview - ios

I need to create two tableviews, a 'parent' and a 'child'. In the 'parent' tableview, the user can create a cell, give it a name, and upon clicking this cell, a second 'child' tableview will be displayed. In the 'child' tableview, the user should also be able to add unique cells to the cell that was tapped. For example, the user is presented with a table view. The user taps the + sign to add a cell, and then names it. The user taps Done and the first tableview appears again with the new named cell in it. The user taps the cell and then creates a cell that is unique to the cell above it. Using Core Data, how can I achieve this? I know how to create a simple tableview, but the part I'm having problems with is having the created cells in the second view controller be unique to where they were created.

It sounds like you need to create at least 2 entities in your core data model. I'll call the first entity Routine objects based on your comment. The Routine objects would have a relationship called "Tasks" with a destination to a Task entity. This would be a to-many relationship, so each Routine object could have many Tasks. When you select a cell, you would pass the managed object represented in that cell to the second table view controller so you can add Task objects to that specific entity.

Related

Getting the data of all TableView Cell which all has TextFields in it (including the hidden views)

I'd like to get every data that is within all cells in one tableview which is quite a long list.
I'm looking for an approach on how to retrieve everything including those hidden in view, which I know the views are reused. I think some of you might have experienced this problem before, what are your approach on this?
I've tried
let cells = self.tableView.visibleCells
then looping into every cell and saving each data to an array but it is not effective in getting those that aren't part of the view or hidden. Is there a way to get over this?
In cellForRowAtIndexPath, YOU are telling the table what is in each cell. So why would you turn around and ask the table what's in each cell? If the user puts "Hello" in your first cell, then scrolls the table enough to push that first cell out of view, then when the user scrolls back to the top, YOU are the one telling it to put "Hello" back in that first cell. YOU own the data source, not the table.
You need a data source. That can be "empty" at first, maybe an array of empty strings if that's what you want (each index in the array could map to a table row for example). But then, as the user interacts with the text fields in the cells, you need to update that data source with the text they entered.
You should use that data source as your source for the cellForRowAtIndex method. That way you can handle populating the cells when they are requested by the table, and you also know all the data when the user is done.
Why not just update the model each time the user taps a key when editing a textfield? You could create a protocol for that cell subclass and make your view controller the delegate for each cell. As long as cells are guaranteed to stay on the screen while you're typing (you'll get some weird behaviors if not) the cell can send a message to the view controller or whatever you hook it up to telling it what new value to store. Then everything is already stored for you when you need the full list, and you don't have to interact with the tableview.

UICollectionView showing selected cells

i have a tableview with form fields, and there is a row that use segue to call an UICollectionView, everything is works except that i can't keep the selected cells (the visual effect that i show when the cell is selected) after go back to the tableview.
I mean, i selected my UICollectionView cells, after that i go back to the form, but if i need to again to my UICollectionView Cells, to deselect o select more cells before submit the data, once the UICollectionView appear my previous selection are there (i print the array, and i see the values) but i cant see the effect that i did for selected cells.
How i can keep the effect for selected cells if I'm going back to select again o deselect cells?
One thing to realize is that this is not going to happen by itself. When you segue back to the table view and the form fields, the UICollectionView is completely destroyed. The next time you show the collection view, it is a new and different collection view.
So, if you want to maintain a knowledge of what the selection was in the collection view, you are going to have to maintain it yourself, deliberately, storing it somewhere when you know that the collection view is being destroyed.
That way, the next time you show your collection view, even though that will be a completely new and different collection view, you can pass the knowledge of what the selection was to that collection view as you create and show it. The user will have the illusion of "returning" to the collection view, in the same state, but in fact you will have saved and restored its state.
It is then just a matter of reflecting the selected state in the collection view's display of its items. To do that, you need to configure your model so that when cellForItemAtIndexPath: is called, each cell looks selected if that item is supposed to be selected.
Every time you use segue to call an UICollectionView a new instance of UICollectionView is created and hence states of selected cells doesn't get restored.
I believe u must have taken an array of index paths of selected indexes to show the visual changes in the cell.
Make your tableView class as delegate of UICollectionView. Using its delegate methods return the selected index array to tableView class .And before pushing to UICollectionView send the same array of index paths back to the UICollectionView. Hope it helps.. Happy Coding.. :)

In Swift, how should I save data from a custom view so that it is not deleted when a cell is dequeued?

I have a custom view that exists in a cell in a tableview. The view is called bulletRow and it is a series of bullets that can be filled in or emptied when a user taps on them. Each cell in my tableview contains some bulletRows and I need to save them when the user taps on them. I have considered using Core Data, but I don't need them to persist when the app is shut down, I only need it to exist when the user scrolls past the dequeueing point.
Here is my situation right now: The default state for bulletRows is to have 5 dots, all of them empty. When a user taps on them they become filled. If the user scrolls down however, they get reset back to being empty. How can I save the state of the bulletRows?
The bulletRows have a property called numberOfFilledCircles which can be set at anytime to change the amount of filled in circles. This is all done in Swift as well.
In general, you should use something, such as an array, to hold the state of your table. The cells in your table should reflect that state, and update that state when selected.
You might start with an array of integers in your table view controller, like this:
var numberOfFilledCircles = [Int]()
Use the number of items in your array to determine how many rows to display in your table, by returning numberOfFilledCircles.count from your numberOfRowsInSection method.
You can populate the array in viewDidLoad. If you're hardcoding the rows, you can repeat this statement for as many rows you want:
numberOfFilledCircles.append(0)
Each Int in the array holds the value representing how many circles are filled (initialize to 0).
In your cellForRowAtIndexPath, use the appropriate value from your array when constructing your cell. For example, if your cell had a UILabel called numberOfFilledCircles, you would do this:
cell.numberOfFilledCircles.text = String(numberOfFilledCircles[indexPath.row])
Finally, in your didSelectRowAtIndexPath, update the array with the number of circles you want filled in:
numberOfFilledCircles[indexPath.row] = //whatever you want
The issue here is that the UI is not the model.
When the ui elements are pressed, you should send an action to the underlying model to update its state, and when cells are dequeued you should restore the checkbox state from the appropriate model element.

Outlet Collection Event

I am creating a settings menu and wanted to implement functionality so that when a user toggles any of the switches, it automatically saves their selection in my sqllite database.
I was thinking about putting each element into an outlet collection, iterating over each element, and adding each event to the same function.
Is there an easier way to do this or is that about it?
Thanks!
I'd put it in a tableview.
Then each cell manages its own UISwitch. The UISwitch also talks back to the cell.
Then in the cell you can call back to the table view controller to update it (with the row index) when the switch is updated.

How can I prevent selection of UITableView from changing when details change and the tableview reorders?

In a core data app, I've got a UIViewController that has a "master" tableview on the left, with a set of detail controls (textfields, switches) and another UITableview on the right. (This is the iPad layout.) The tableviews are populated by NSFetchedResultsController instances. When the user selects a row on the leftmost tableview, it populates the details fields accordingly for editing, and the far right table is populated by another NSFetchedResultsController, displaying a set of related objects.
My problem is that for the master tableview, the one on the left, whenever I edit on of the fields related to the sort descriptors of the NSFetchedREsultsController, the table reorders and changes it's selection, which means the user would have to find the desired row and select it again to continue working. Is there a way I can prevent that selection from changing, or at least force it to reselect the proper row after it reorders the data?
It sounds like you are implementing the fetched results controller's delegate in only the most basic way, i.e. calling reloadData from within the controllerDidChangeContent: method. This will clear the table view's selection.
It you implement the fine-grained delegate methods, you should receive multiple changes of type NSFetchedResultsChangeMove during a reordering update, from which you can call moveRowAtIndexPath:toIndexPath:, which should preserve the table view's selection.
Otherwise you can store the model object that is selected in controllerWillChangeContent: and restore the selection from that model object in controllerDidChangeContent:.

Resources