I have a UIViewController that contains a UITableView.
I'm trying to fill this table with data parsed from an XML file.
The first time the TableView loads the data isn't available just yet.
What would be the best way to reload this view?
I tried to do a self.TableView reloadData after the parsing finished.
Unfortunately the NSMutableArray holding the parse result getting reset during reloadData.
The array is set at the UIVivewController.
Should I set the NSMutablearray as a global variable?
THanks a bunch
The array should be an ivar (instance variable) of the view controller containing the UITableView. When you are done parsing the ivar should be populated with the data and you should be able to populate the UITableView with no problem using [self.TableView reloadData]. Make sure you are not removing all objects or re-initializing the array in any of the delegate or datasource callbacks.
Don't make it global, make it an instance variable. As in:
#interface MyViewController : UIViewController {
NSMutableArray* parsedDataArray; //declare the array here
}
#end
But beyond that, yes, calling reloadData after the parsing finishes is reasonable. Just fix the code to not reset the parse results when reloadData is called.
Yes, take that NSMutableArray as a global for that controller file.
Be sure u are getting value in NSMutableArray in the controller which holds table view.
simply reload table.
Regards,
Shyam
Related
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);
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.
I am stuck in a strange situation , i am getting data from the website using XML files and i am filling an Array (NSMutableArray Type) that i later use to display the data on Table View. The problem is that functions related to UITableView are called earlier and at that time the Array is not filled, this cause the program to crash. When this function is executed arrayData is empty and count functions returns nothing. Is there any way that i call NSXMLParser functions earlier than the UITableView functions.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayData count];
}
Thanks,
Taimur
The array would return 0 if it existed but without containing any objects - so the app shouldn't crash. This means your array has not been initialized yet. You have propably added a pointer to your array as an instance variable and maybe as property, but you still need to create the actual object towards which that pointer should point.
So, if we're dealing with a property of a viewcontroller subclass here, add something like this to your viewDidLoad method:
NSMutableArray *newArray = [[NSMutableArray alloc] init];
self.arrayData = newArray;
[newArray release];
Your situation is not strange at all - it is extremely common when developing apps with asynchronous data loading requirements.
NSXMLParser is a SAX (event-driven) parser - it will parse data when the data is available. It is up to you when you choose to display your table, but obviously if you try to display it before the XML data is available then you will have to take steps to prevent a crash, or at the very least a bad user experience. Typically you would display an activity spinner or a "loading data..." message until the data is ready, and in a background thread load the XML. Once loaded, the BG thread should signal to the UI thread that the data is ready, and perhaps invoke reloadData on the table to load the data.
i am a new iphone programmer i want to create an array that keeps adding a value at its last position when i moving from one view to another
because at last view ,i need that full array which has all values from all views....
i tried taking an array and sending it view to next view (array is defined in all views) but when i tried to put value to it
You can use NSMutableArray class with addObject: in order to add your views into array. Have a single view controller, declare that array and all your views can access the view controller's array.
try to save it to a class (singelton class) it will work....it will surely work if you transfer it to the delegate class....make an NSMutableArray object there and initialize it once in applicationDidFinishLaunching method and then make object of delegate class and transfer your values to it by addObject method.....
The best way is initialize your mutablearray in appdelegate and use the same using the object of delegate in each class wherever you want to use.you can add the object using addobject and can access using property objectAtIndex anywhere.
I am new to iphone development. numberOfRowsInSection method of UITableViewController executed before parsing the XML node so that this method return value as zero, but this method should be return value of number of rows with data.
please help me
thanks.
After parsing of your XML data is finished call [myTable reloadData]; - it will force your UITableView to reload the data shown and thus all necessary methods (including numberOfRowsInSection) to get called.
I've had a similar problem before, and it was because I was passing the array/dictionary to the controller from the appdelegate. Unfortunately, after the app delegate went out of scope, the array was lost.
Try doing an array copy,
NSArray data = [NSArray arrayWithArray: passedInArray];
Sorry, don't have my trusty macbook on me to check the code, but you get the drift.
I solved this problem via made connection between tableView reference and file owner in interface builder.