How do I add specific content from a collection view to another one? - swift

I have three elements in my UICollectionViewCell:
Two labels with name and price, and a quantity button.
I would like to add the name and the quantity with "didSelectItemAtIndexPath" to a specific collection view in the previous view controller and the price in another view controller. I may achieve this by using a segue to pass data to any view controller of my choice.
Also, at the same time, I want to keep track of the selections I make adding them to a table view below the collection view.
My guess is to create empty arrays for each item.. one for name, price and quantity.
I may be wrong.. and I tried to append my current selection and I know I am missing something.

To pass information to a previous viewController use a delegate protocol. Suppose a is your first viewController, and b is your second viewController.
You can create a delegate protocol in b (here you specify a func you want to execute inside of a)
Set a as the delegate for b
write a function yourDelegateFunction in a , which your delegate protocol will call to update the viewController with your first collection.
in didSelectItemAtIndexPath call delegate.yourDelegateFunction
Here's a video with an easy guide for how this works:
https://www.youtube.com/watch?v=3BcBu30thIA
If you provide a sample of your code, and highlight where you experienced issues, I can provide a more precise solution.

Related

How BehaviorRelay.accept works in rxswift

I am drawing a tableview via BehaviorRelay.
Currently, I am using the code below as a way to add data.
viewModel.user.append(Person(name: "king", phoneNumber: "12341234"))
viewModel.personObservable.accept(viewModel.user)
I wonder if this code changes the user itself so that the whole tableView is redrawn.
If so, what method can I use to change only the data I added?
The code presented causes the personObservable (which is actually a BehaviorRelay apparently,) to emit a next event that contains an entire array of Person values, not just the latest Person added. Importantly, it's not emitting the viewModel.user object (at least not conceptually) but an entirely different object that happens to be equal to viewModel.user.
The default dataSource, the one that you get when you call items with anything other than a DataSource object, will call reloadData on the table view. This doesn't cause "the whole tableView" to be redrawn though, but it will cause the table view to query the data source for all of the visible cells, even if they haven't changed.
If you only want the table view to load the new cell, then the data source object needs to be smart enough to compare the new array with the array it's currently displaying so it can figure out which values are different and add/remove/move cells as appropriate, instead of just calling reloadData. As #Sweeper said in the comments, the RxDataSources library contains a set of data source classes that have that logic built in. If you wanted to reinvent the wheel, just write a class that conforms to both RxTableViewDataSourceType & UITableViewDataSource and implement the diffing yourself.

remove top left back button

I would like to remove the top left back button.
I tried to check "Full screen" option, but the arrow is still here.
I want to remove the back button because I have a button next to this button, and I don't want the user tap on it by mistake
Thx
According to the Apple Documentation the deprecated method that was in the previous answer (which used to solve this issue) has been replaced with:
reloadRootPageControllers(withNames names: [String],
contexts: [Any]?,
orientation: WKPageOrientation,
pageIndex: Int)
"Parameters
names
An array of NSString objects, each of which contains the identifier of an interface controller in your storyboard file. The order of the identifiers in the array defines the order of the corresponding interface controllers in the page-based interface.
contexts
An array of objects of type id. Use this parameter to pass context objects to each of the interface controllers loaded into the page-based interface. The first object in the array is passed to the first interface controller, the second object is passed to the second interface controller, and so on.
orientation
The scrolling orientation for the page-based interface. For a list of valid values, see WKPageOrientation."
And pageIndex should be pretty simple to figure out. If you only have one element in [names] it will be 0. Otherwise you should select the index of the page you want loaded from [names]
Try:
WKInterfaceController.reloadRootControllersWithNames(["myInterfaceController"], contexts: [])
This removes it by making the controller the root controller. You may have to reload the controller after calling this.

Segue from and to the same UITableViewController (navigation) - controller reusable?

I am new to iOS development, and I wonder how to do the following task:
I want the user to navigate through a multi-level menu. I have UITableViewController with two different kinds of cells.
When clicked on cell type number one I want to reload the UITableViewController with different data. (A click on the other cell does something else.)
Do I have to set up a different UITableViewController in the StoryBoard or can I reuse the existing one and segue from and to the same one? I don't know how many levels I will have in the end because the data comes from a remote source. I could have two different UITableViewControllers (because there is one different button), but I'd prefer to do it all in one (and try to hide and show the button when needed).
Anyway, I need to reuse the same class but change the data for each instance. How can I do that? I also need to restore the old state (last used data) of the controller when going back - or does this happen automatically - is it an "unwind" anyway?
Which methods should I override to delegate the new data?
I tried several tutorials but couldn't yet find the information needed.
Thanks in advance
Read up on Nav Controllers, I'm pretty sure this is what you'll use.
Add UITableViewDelegate to your ViewController press command and click on it for more information.
You can always just use a simple variable to keep track.
Declare a variable var step = 0
Then in your
override func prepareForSegue(){
step += 1 //and pass that variable to the next view and so one, incrementing by one each time.
}
Then in your
viewDidLoad(){
switch step{
case 0:
//add this data
case 1:
//add this data
case 2:
//add this data
default: break
}

Second TableView not displaying any/correct nest array values

I have no errors but my second table view controller simply won't display the correct values (or number of values) segued from my first table controller.
Here's the full project if more insight is needed - https://www.dropbox.com/sh/77gs3bmxb77ul79/AAApV2m8Vh9Sc2FABiDzZRgha?dl=0
anyways, my code for the first table controller and second controller are shown below.
I have a feeling my issue is occurring with the naming conventions of my struct and new variable in the second controller "newrestaurant", in the prepare for segue function, or the "newrestaurant[indexpath.row].name" section of the cellforrowatindexpath function in the second controller. Otherwise, the other code appears pretty straightforward.
Any help would be incredibly appreciated!!!
You forgot to assign segue identifier.
Click on your segue and Attribute Inspector at right hand side and assign segue Identifier as shown in below Image:

View-based table bindings in Swift

I'm trying to set up a view-based table in Swift using bindings. All of the examples I've seen use a datasource/delegate setup.
I have an array of Flag objects which has two properties - flagName: String and flagImage: NSImage. I have an NSArrayController managing this array.
If I set up a cell-based table, and bind one column to arrangedObjects.flagImage and the other to arrangedObjects.flagName, I get a table displaying images and names, and I can use the array controller's add and remove methods, so there are no problems with my datasource or my array controller.
I have been following the instructions in Apple's TableView Programming Guide to bind my view-based table to my array controller:
tableView Content binding: FlagController.arrangedObjects
textField Value binding: TableCellView.objectValue.flagName
imageView Value binding: TableCellView.objectValue.flagImage
(IBs autocomplete is not happy with the paths for objectValue.flagName respectively flagImage; it doesn't feel that there should be any completion whatsoever and says it can't resolve the path, so it looks as if the problem is with the tableView's content.)
If I do this, my table has a number of rows that corresponds to the number of elements that my array controller is managing at that moment (I have two simple setups, one object vs. 50 objects, so it's clear that something is bound). What I don't get is a display; and selecting a table row does not seem to send back a message to my flagController.
What am I missing? Has anyone been able to make this work? I've had no problems with other bindings in Swift so far, but I'm starting to think that the sudden reappearance of datasource examples is not unrelated to this.
It sounds like you've failed to implement the delegate method -tableView:viewForTableColumn:row:. That's a common cause of blank tables.
You don't actually have to implement that if you make sure the identifier of the table column and the identifier of the table cell view are the same in IB.
Otherwise, the method can just do this:
- (NSView*) tableView:(NSTableView*)tableView viewForTableColumn:(NSTableColumn*)tableColumn row:(NSInteger)row
{
return [tableView makeViewWithIdentifier:#"TheIdentifierOfYourTableCellView" owner:self];
}
(It could also do more, if desired.)