swift: send value back to UITableViewController from dynamic cell with UIDatePicker - swift

I've got a dynamic table with a cell for a UIDatePicker. There's a function in the UITableViewCell class that updates a variable when the picker changes. I'm just having trouble getting that value back to the UITableViewController class. I can print the correct variable while spinning the picker, but when I go to reloadRowsAtIndexPaths for the tableView I need the value available.
Since there's no segue, I can't get a protocol/delegate to work. I imagine a global variable might also work, but that's cheating. Any suggestions?

You can use NSUserDefaults for storing values globally which can access anywhere into your project. you can set values this way for your picker.
NSUserDefaults.standardUserDefaults().setObject("YouValueForm", forKey: "YourKey")
After that you can access this value anywhere in your project this way:
let yourVar = NSUserDefaults.standardUserDefaults().objectForKey("YourKey") as! String
You can modify this as per your need. and once you got your values you can do reloadRowsAtIndexPaths.
Hope this will help.

Related

Passing variable in viewDidLoad() or ViewDidAppear() to button action

I have a complex variable that I look up from a Firestore Document but that is not important. It works perfectly in either viewDidLoad or viewDidAppear. I can print it to verify the variable has the correct value.
let q1 = data!["q1"]! as? Bool ?? true
When I call the variable in the if statement in the button action I get the following error:
Use of unresolved identifier 'q1'
print(q1)
But print(q1) works perfectly if run in the viewDidAppear.
Can someone tell me what I am doing wrong? How can I pass a variable from viewDidLoad to the button action?
How can I pass a variable from viewDidLoad to the button action?
Store the value in an instance property. Properties are global to all methods, so viewDidLoad can see it (to store the value in it) and the button action method can see it (to retrieve and use it).

Nil optional unwrap error when firebase value gets set in UICollectionViewCell

I call firebase database to retrieve names of locations. I store the data in an array of dictionaries, then populate collectionView cells with the data from the array. when the collection view cell is selected, I segue to another view controller and pass the data to a collectionViewCell inside the view controller.
The console prints the correct values, but when I set a UILabel text equal to the value, the compiler throw a nil optional unwrap error.
Passing the data to the view controller in didSelectItemAt:
Setting a value to a UILabel text:
Console prints the value:
Am I passing the data incorrectly?
Im not sure why the console is printing the value, but compiler throws an error. Any insight as to why this happens, and how to correct this is very appreciated.
EDIT: ozzieozumo was correct, I was incorrectly instantiating my cell class. I was creating a new instance of my cell, but had no connection to storyboard, which led to my label being nil.
SOLUTION: I edited my segue method to take my dictionary as a parameter. instead of instantiating the CollectionViewCell in the method, I instantiated the CollectionViewController and passed my dictionary to the CollectionViewController. In the Controller under cellForItemAt, I set pass the data to the cell.
The assignment statement fails because the left hand side is nil, not the right hand side.
Your outlet is nil because of the way you are instantiating the cell instance.
let detailImageCell = LocationDetailImagesCell()
That will create a new instance of your cell but that instance has nothing to do with your storyboards/xibs, and so the outlets will be uninitialized.
That is your immediate problem here.
The label is not linked in the Interface Builder, it is that element that is nil

Storing a variable in swift after passing it

I have a variable that is passed between two tableview controllers.The cells in the first tableview have a detail label that is updated by this variable that is passed from the second one. once the string is passed i want it to save and bump out the old string.I would like the variable to stay saved until the app is uninstalled. any idea on how i would do this ? passing the data with protocol. I appreciate any help!

How to implement a uitableviewcell who's value is derived from another uitableviewcell

I'm trying to do something thats fairly simple but I can't see the best way to do it. I have a uitableview with two cell's, the first has a uitextfield in the contextview with the input view set as uidatepicker. The cell maps back to a nsmanageobject date property. The nsmanagedobject has a dependent property which is the difference in days between todays date and the date selected. This value is displayed in the second cell when the view loads.
The problem is that when the user changes the date in the first cell the second cell is not updated automatically.
Firstly I thought I could just call setNeedsDisplay on the uitableview. But this seems rather heavy handed.
So I've been reading up on KVO and I've not managed to find a good example of a solution to my problem yet.
I'm using fetchresultscontrollers for my tableviews. I tried to implement a key path dependency as shown in the iPhone docs but this hasn't been successful.
for example...
+ (NSSet *)keyPathsForValuesAffectingDaysRemaining
{
NSLog(#"keyPathsForValuesAffectingDaysRemaining");
return [NSSet setWithObjects:#"dateOfOccasion", nil];
}
-(NSNumber *)daysRemaining {
return [self.dateOfOccasion daysFromNow]; //simple calendar calc.
}
Could someone point me in the right direction of an appropriate solutions to a relatively simple problem.
Thanks,
Gary
I think implementing a [[self tableView] reloadData] strategically should fix the problem. As soon as you change the object in cell1, I would assume you basically save the change globally or in Coredata. Simply reloading the tableView in that configuration should fix the issue.
In your case, since you are implementing the NSFetchedResultsController , I would assume objectDidChange would fire when you change the value of that object. A tableView reloadData would be a nice implementation there !
If you have the modified value , you could reload the row to update the content. You can reload row in your KVO method after updating row's value,
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);

A Question about Assign Properties

Edit 2: What I previously planned was probably a bad idea and I now changed my design: My UITableViewController has an array with all the values of my UITextFields and I am using delegation to update the values in the array. (If a value in one UITableViewCell changes, I send a message with the new value and the index of the cell).
Original Question
I would like to create a UITableViewCell subclass. To access my cells, I would like to have an NSMutableArray in my UITableViewController with all the cells. Whenever I create a new cell in - tableView:cellForRowAtIndexPath: I would add it to the array. The cells should however know about this array. I would declare a property like this for the UITableViewCell:
#property (nonatomic, assign) NSMutableArray *cellsArray;
Whenever I create a new cell, I would set its cellsArray to my array.
My (probably simple) question is: Is it correct that cellsArray will hold a pointer to the array in the UITableViewController and when I add stuff to the array of the UITableViewController, the cells will know this too, i.e. can access it?
Edit: The UITableViewCells contain UITextFields. I used to rely on the -cellForRowAtIndexPath: method and the visibleCells array, however when the cells moved out of view, the content of their UITextFields would also be lost. I then decided to store the cells in an array. When the user taps save, I iterate through the array and store the values. Also, I would like to automatically update the enabled property of the save button, depending on whether all cells contain something - for this I need all cells, too.
The cells should know about the other cells so that they can select the next cell when the return/next key on the keyboard is pressed.
If there are better approaches to this, I am glad to hear about them!
Not a direct answer of your question, but this sounds like a very bad design. Why should one cell need to know about its siblings? Any event/change that occurs in one cell and has an effect on the other cells should be handled by the table view controller. The single cells should be separate entities that should have no need to know about the state of each other.
Secondly, there is no need to introduce another array to manage the table cells. The table view already has a property visibleCells that you can access from the table view controller. And should never have to interact with invisible cells anyway because those are managed by the table view and its reuse facility.
I believe the answer is Yes.
My understanding of assign is that you can assign a value to such a variable and the retain count for the original object is not incremented. Similarly you need not release the variable in the object's dealloc method. You may run the risk, however, that the original array goes away and then cellsArray is pointing at something that is no longer there.
I use assign when I want to pass a reference to an object to another object (e.g. a view controller that is going to display or otherwise manipulate the object). And in the latter object, I do not release it's pointer to the object.
You also see assign used with properties that are id's, like
#property (nonatomic, assign) id<SomeProtocol> _myDelegate;
All that being said, with the exception of the id case, often I feel "safer" using retain for the property and being sure to release in dealloc. :-)
Anyway, I think that's the crux of the difference.