Moving cells from one table to another in iOS - ios

I am trying to create an application with multiple table views that uses and implements Core Data. I would like the user to be able to select cells in one table and move them to another (like in Apple's mail application) using either a check accessory or a selectedCell method with an action sheet. I'm stuck because I don't know if you are actually moving the cell to another table or if you are adding a copy to the new table and deleting the original. Basically, I'm asking for a basic example of cell movement to give me a push in the right direction.

You won't be moving cells. The model for a table view is an array. Move things between the arrays and tell the tables that their model has changed.
id somePartOfMyModel = [self.arrayA objectAtIndex:someIndexPath.row];
[self.arrayA removeObject:somePartOfMyModel];
[self.arrayB addObject:somePartOfMyModel];
// the simplest, non-animated way to update the tables.
// I'd advise getting this working first, then later trying fancier UI to indicate changes
[self.tableViewA reloadData];
[self.tableViewB reloadData];

You would not be technically moving the cell to the other table. The way I would go about doing this would be to pass the NSManagedObjectContextID of the item between the tables, depending on how large your entities are and if the tables are in the same view controller.

Related

Using KVO to update table cells from model upon any change to model

I may have multiple table cells which are updated from the same model. I want to be able to update all of them when the model changes. So I assume I will use KVO to do that. My question is, how can I have each react to any change to the model, not just the part that each cell will display? i.e. if one item in the model changes, I want all of the cells to update themselves.
try to use tableView.reloadData() immediately after changing the model. On the other hand you may also try to use RxSwift and bind your model items to tableView - your tableview will then reload whenever the model changes. Refer to these articles as simple examples of RxSwift usage with tableView:
http://rx-marin.com/post/bind-multiple-cells/
https://daddycoding.com/2020/04/20/rxswift-uitableview-with-section/
So I assume I will use KVO to do that.
Table cells are very transient -- the cell for a given row in your table will get reused for a different row almost as soon as it scrolls off the screen. So long-term relationships really aren't something that cells are good at, and you probably don't want to go setting a cell up to observe part of your model directly. Instead, let the view controller worry about the model and reload any affected cells when the model changes. If you want to use key-value observing to let the view controller watch the model, that'll work fine.
My question is, how can I have each react to any change to the model, not just the part that each cell will display?
When the view controller learns that the model has changed, it should tell the table to reload its data. At that point, the table will go through the process of recreating the visible cells, and the effect will be that all the cells will appear to "update themselves."
Please try to reload table by using tableView.reloadData() after the data changed.

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.

With IOS, is there a way of making a proper table with rows and columns?

I have been trying to find a way of creating a table, with rows and columns, for iOS. The UiTableView is basically a tree-structured list like this
Apple sample code, but because it's called a table view, it hijacks any attempt to search for a proper table.
There are a couple answers that suggest very clunky ways of doing it, like this SE Question, but it's quite old. Has the situation changed since this question was asked?
I've done this using UITableView by simply designing custom UITableViewCells that are segmented into columns. All the custom rows together certainly give the appearance of a multi-column table. When any of your data in a column or row updates, you re-render the tableView or have each row in charge of its own re-rendering (i.e. via an NSNotificationCenter message), and the whole experience for the user is as a multi-column table. If you setup your table's data as a separate row-column data model, it's pretty easy to get all the individual cells to go where they need to go with your custom UITableViewCell.
take a UITableView and on tableRow take 3 view of(width=1 and height=row height), each view ill be at equal distance from another one. and take label between these view at end it ill look like a table with rows and column

How could I drag a tableview from up to down and fire an action then?

I have in mind following behaviour for my table view controller. It is a table view to show to do items, just like a tasks list. I want to know if it where possible to drag down the whole table view and hold it in this position, and while in this position the user could enter a voice command that should be stored as a sound file in a core data entity.
Any proposals and help is welcome.
There is a project that implement PullRefresh, but you can use this to do another stuff.
Take a look to EGOTableViewPullRefresh

Load up entity from db rather than pulling data directly from db

I created an iOS app that allowed me to enter data into database and then display in tableview (using NSFetchedResultsController & tableView cellForRowAtIndexPath). This worked. Now, I added 3 one-to-many relationships and another entity and I need to load up the entity (on the one side of the one-to-many) from the db instead of pulling it directly from the db so that the data can be used in the relationships. Do I still use NSFetchedResultsController?
relationshipEntity2 is the one-to-many relationship between Entity1 & Entity2. The many point to Entity2
I know I'm supposed to use:
Entity2 *entity2 = [[self.entity1.relationshipEntity2 allObjects]
objectAtIndex:indexPath.row];
I am new to iOS development and even newer to Core Data but I must learn it. Any bit of help or pointing to a book or tutorial I haven't come across yet would be greatly appreciated. I haven't had much luck finding anything that does what I'm supposed to be doing.
Thanks and have a great week!
-------added for more description on project-------
I'm given the task of having a View Controller with a 3-part segmented button. There is also an add UIButton that pops up a view (bringing subview to the front, not a popover segue) with a UITextField for input to add to the table view on the view controller. There are 2 entities and three 1-to-many relationships. There is 1 relationship for each button on the segmented button. If the user, has the first part of the segmented button selected, adding a value to the popup textbox, should only add it to the table view seen when the first segmented button is selected. I have the CoreDataGeneratedAccessors created. I had this project saving data to database and fetching data to present in the table view but that was before the segmented part was added. Now I have to figure out out to separate the data into "collections" and then show all of say the first collection when the first segment of the segmented button is chosen. I'm just learning and just figured it out without the segmented part and now it's changed on me. I've worked with relational databases but in iOS it seems like it's new again.
Your question is by no means clear. You are not describing any problem, nor do you explain intelligibly what you want to accomplish.
Trying to infer your meaning: yes, you would continue to use your fetchedResultsController. This controller gives you the right object for each indexPath. When building your cell contents, you can easily get to the relationship entities with entity.relationship.
The code above will not work because entity.relationship returns an NSSet which is an unordered group of objects. (The additional allObjects does nothing and is redundant.) Therefore, objectAtIndex will not work (this only works for NSArray objects).
Hope this helps.

Resources