In a cell I have
def function():
stuff
In a cell below the previously mentioned cell, I call
function()
The error I get is:
name 'function' is not defined.
How do I call a function in another cell in iPython?
Run the cell in which you defined the function first, then run the cell you call it from. If you make any changes to the function later, run that cell again for the changes to take effect, otherwise your calls will run the un-changed function.
or, as #ymonad pointed out, run all cells each time. it takes care of the trouble as long as function definition comes before any point it is called upon.
Related
Update:
This is an attempt to improve this question. I have a solution, which I will post as an answer shortly. Its based on the comments I received below. Hopefully this improvement, along with the comments and subsequent answer, can help someone else who might fall into the same trap I did.
I have a collection of objects that need to download some images before I consider them "ready".
I attempted to handle this by passing in a "completion handler" into the init function, which I in turn would pass it to the completion handler of the function that would actually download the images. The intended net result being that after the object was initialized and loaded with it's images, then the closure I passed in during initialization would be called, in this case a notification that the object was "ready"
To accomplish this I tried to capture the newly created object in the closure passed in as the completion handler. (shown below in trailing closure format)
for marker in markerList {
var ourLatitudeMapMarker = OurLatitudeMapMarker(size: size) {
NSNotificationCenter.defaultCenter().postNotificationName("OurLatitudeMapMarkerReady", object: self, userInfo: ["ourLatitudeMapMarker":ourLatitudeMapMarker])
}
}
This results in the error "variable used within its own initial value".
My original, poorly phrased question, was essentially:
How can I get the newly created object into the completion handler passed to the init function so that it can be used in the completion handler when the object finally, is "ready"
In hindsight I think my question should have been:
How can I call a closure when my objects are "ready"? "ready" being initialized and loaded with their images.
As jtbandes pointed out in his comment, one solution to this could have been to simply pass self in as an argument to the the completion handler, but in the end I agreed with both nhgrif's and zaph's comments.
The solution was obvious, once I realized that being initialized and being ready are not the same thing. So I just have init, initialize the object and another method loadImages, takes the completion handler, which is passes to the method that actually down loads the images, and calls the completion handler when the objects are ready.
So now instead of one statement that attempted to do everything (initialize and load the images), I have two.
let ourLatitudeMapMarker = OurLatitudeMapMarker(size: size)
and then later do something like this
ourLatitudeMapMarker.loadImages() {
NSNotificationCenter.defaultCenter().postNotificationName("OurLatitudeMapMarkerReady", object: self, userInfo: ["ourLatitudeMapMarker":ourLatitudeMapMarker])
}
I have a TreeTableView that is working great. I have implemented a context menu that appears when the "TreeTableCell" is clicked. In my context menu, I have a MenuItem called "edit" that, when selected, I want to cause the current TreeTableCell to start editing. Unfortunately, I am unable to figure out how to correctly start editing the cell.
My initial thinking was that since the context menu callback is in the scope of my TreeTableCell instance, I would call the "startEdit" method of my cell. This appears to work ... however, when the commitEdit occurs, I get a NullPointerException deep within the internals of JavaFX. This makes me think that explicitly calling startEdit() is the wrong thinking and that there should be a correct way to cause a specific cell in the table to begin its edit cycle.
I was able to find the answer after lower level study. I had assumed that I could call startEdit() against my TreeTableCell however that is not the correct way to flag a table cell as entering is editing mode.
Instead both TableView and TreeTableView expose an edit() method that take two parameters:
The row number of the table indexed from 0
The TableColumn of the table
The combination of row and column provide a unique location of a cell in the table. Calling edit() places the cell in editing mode.
Here is the JavaDoc for TableView edit method:
JavaDoc for TableView edit
I'm wondering what is the best approach/design when using custom UITableViewCells.
One way I'm doing it is having a init that takes all parameters necessary to setup the UITableViewCell. Like -initWithLabel:groups:error
Then I would init it like CustomUITableViewCell *cell = [CustomUITableViewCell alloc] initWithLabel:#"Hello" groups:#[#"1", #"2", #"3"] error:NO];
Then perhaps later if I would need to update the error of the CustomUITableViewCell to YES I would just reload the cell for CustomUITableViewCell and supply YES in the init.
Now I'm wondering if it would be "better" to have a setter for the cell, like - (void)setError:(BOOL)error that I can set whenever I need to update the error of the cell instead of reloading the whole cell.
What would be pros/cons comparing the two options?
Your first choice doesn't seems to be a good approach. If you are creating cell each time it just fine. But actually happening is table view will create only 10-15 cells(depends) and later they will be reused. So initialization is no going to perform unless you call it externally(that is not good).
So the good approach in custom cells are use the properties and expose all the things that is going to change in cellForRow of your datasource object. Make your labels, error... as properties if it is accessible to the outer class. So you can easily change any values.
When you have a member variable that can change, then for sure it should use a setter (and be a property), so you don't have to replace the class each time. The source-code will be clearer, and the program will be more efficient. You might also include it in your init, if you think it's a good idea to encourage clients to explicitly set that variable when they make a new object.
Simple question.
I have a tableViewController that will display a list of search results.
I want to create a block based callback (I think that's what you would call it)
I would present the viewController
[searchResultController showSearchResults //BlockCode here {
//did select this item...
}];
but use a block so that I can detect the selection made from the tableview instead of using a delegate method.
Problem is I have no idea how to implement this. Is there a good tutorial or a simple example on how to do this?
Your search results controller needs to have a block property. This block should be defined with no return type, and should take a parameter of whatever object you are going to use to represent the selected item.
Before presenting your search results controller, set the block property to whatever you wish to do for your callback.
Within the search results controller, when a row is selected, execute the block, passing in the selected object. Either this method, or the block itself, should also dismiss your controller.
I'm a c++ programmer new to objective-c.
I created a calculator app that is working fine using a single view. I have a Calculations class and a ViewController. Every time a button is pressed, an IBAction method in the ViewController calls methods defined in the Calculations class to handle the input and returns the output as an NSString which I then set as the value of the label.text field.
Now I am working on a tab bar app using the same Calculations class. This app has two tabs, each with a unique set of input buttons for the calculator (both views sharing the same input/output data). The first tab is identical to my first app with the single view, so I am trying to do this in a similar fashion.
Here is the problem:
When a button is pressed, the IBAction method that handles the input runs through the calls to the Calculations class methods (shown below) without error:
-(IBAction)readInput:(id)sender {
[_calculations input:[sender titleForState:UIControlStateNormal]];
inputField.text = [_calculations inputDisplay];
outputField.text = [_calculations outputDisplay];
}
however, both the inputDisplay and outputDisplay methods return nil. Using the debugger I noticed that I am unable to "step into" the calls to _calculations methods, instead the line is skipped and the value returned by both is nil. I added the following method:
-(IBAction)setNumber:(id)sender {
NSString *button =(NSString *)[sender titleForState:UIControlStateNormal];
inputField.text = button;
}
and if I attach this to the input buttons I can see the display updated. This seems to be an issue with calling the _calculations member functions and tab bar views (because this issue is not present using a single view).
I realize that I left out a lot of information, but I did it to avoid providing irrelevant information. I will provide all details that are necessary if asked.
Check to make sure _calculations is not nil.
You can send any message (call any method) on nil and it will just return nil, not cause an exception.
Without seeing more code it is going to be a bit difficult to diagnose.
If I was trying to debug this issue I would first make sure _calculations points to the object you want it to point to. If its loaded from a NIB then it might not be getting initialised, and still be nil. You can send messages to nil objects without any issues. If an object receives a message that it cant handle (the method doesn't exist, or the target object is nil) then the return for that call will be nil.
I have in the past put initilization code into the init: method, and spent a few hours why it wasn't being called, until it dawned on me that I needed to put my init code into the viewDidLoad:, or the initWithNibName:bundle: or even the initWithCoder: selector.
HTH, Matt