Where to load my array? - ios

- (void)viewWillAppear:(BOOL)animated
{
goals = [[NSArray alloc] init];
goals = [[self.operation valueForKeyPath:#"goal.goalNaam"] allObjects] ;
[self.tableView reloadData];
I'm loading my elements for my tableview in viewwillappear, as you can see in the code above. When the user adds something to the table (adding happens in a different view), it's added to Core Data and the array is loaded when the tableview is again appearing (after the dismissed "add-an-element-view"), now I want to delete something from the table. But the problem is that we're now staying in the same view (the tableview), so my array is not reloading (cause the viewwillappear is not executed). Anybody an idea how to solve this ?

You can call "[self viewWillAppear:YES];" after delete the data from Core data, So the datas are reloaded from the same view (Tableview)

You load your data in cellForRowAtIndexPath. This is called as cells come into view, and only cells in view (or almost in view) are loaded.
To force something to be reloaded, use reloadRowsAtIndexPaths or one of the other reload... methods.

Check out NSFetchedResultsControllerDelegate . You can set your VC as the fetch result delegate, and be notified when the data structure changes.

Related

iOS UITableView behaviour broken when not active

My current set up is the following:
Root Tab Bar:
Collection view with magazines
Bookmarks (with a table view)
Others
You can add a bookmark from a magazine in the collection view and also remove it from there.
The behaviour I'm seeing is the following:
I start the application, the table view queries the number of sections, number of cells, but not the cellForRowAtIndexPath. I could understand why, as there is no cell in the active view, so no data should be loaded.
When I add a bookmark from the collection view, it adds it to the array (via a notification) and requests the tableview to be reloaded. As there isn't an initial entry, it goes through the motions described above. When I press it again to remove the bookmark the entry is removed from the array. This is where it gets interesting because the first thing the table calls is not the number of sections or rows but the cellForRowAtIndexPath. As the array is empty, the application crashes on a request for data on index 0.
My question is why does the cell creation get called in that order? Is there any way to avoid it?
If you changed the section, try calling - (void)reloadSections:(NSIndexSet *)sections before you call reloadData
https://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UICollectionView_class/index.html#//apple_ref/occ/instm/UICollectionView/reloadSections:
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationLeft];
The reason this was happening is because I was attempting to change something about the table before I reloaded the data.
[self.tableView setSeparatorStyle: !_helpText.hidden ? UITableViewCellSeparatorStyleNone : UITableViewCellSeparatorStyleSingleLine];
[self.tableView reloadData];
That was for removing the lines so a message can be displayed. However that update was using old data as reloadData had not been called.
[self.tableView reloadData];
[self.tableView setSeparatorStyle: !_helpText.hidden ? UITableViewCellSeparatorStyleNone : UITableViewCellSeparatorStyleSingleLine];
Reversing them fixed the issue.

How can I reload only updated cells from UICollectionView

I have several cells that are written onto a UICollectionView and reloading them takes a long time. How can I reload only when the data of a cell is modified? Here's one thing I've tried:
if([oldDataSource count] != [currentDataSource count])
{
[collectionView reloadData];
}
Yet, I still don't want to deal with reloading the entire thing. I've also tried:
if([oldDataSource count] < [currentdataSource count])
{
//something was added. //reload the last item in the current data source
}
This also failed because I can't reload something that doesn't "exist" in memory yet. This method also doesn't work because if something was deleted from my data source, the Cell will still remain in the interface, and will crash if clicked on.
So, what's the best way to deal with edited cells?
Thanks!
Reloads just the items at the specified index paths.
- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths
Discussion
Call this method to selectively reload only the specified items. This causes the collection view to discard any cells associated with those items and redisplay them.
try and get the cell u want to change. then:-
If u can get it. change the data source and reload it.
if it doesnt reflect the changes. change the data source, then the repective new values of the cell, then call setNeedsDisplay on the cell.
If the cell is not returned (that means) it is out of the view. Simply change the data source. The change in the data source will be reflected when the cell is brought to view
try any of these three.
cheers. have fun

Reload TableView Tab Bar Application

I have a tab bar application that displays tableviews in two of the tabs. The tableviews are populated using NSFileManager to read the contents of a plist file stored with user data. One of the tabs displays the "complete" items, the other tab displays the "Incomplete" items. As the user selects a row, a page with the details of the item selected is displayed. In the incomplete table there is a button the user can press to move these details from the "Incomplete" table to a "Completed" table. The plist is then updated using NSFileManager to change a field detailing if the item is in the complete or the incomplete list for this item.
The problem I have is that the changes are not updated on the two tables just by selecting between the two tabs. The user has to quit the application and start it up again to see the item moved from one table to the other.
The data seems to be updated its just the view that doesn't update.
I'm new to cocoa so any help at all would be greatly appreciated.
As you haven't provided the code, so i cannot tell where exactly you should reload your tableview.
You have to call
[self.tableView reloadData];
just after you are finished using NSFileManager to save your data in the plist.
Table views are not updating because you are probably not calling [self.tableview reloadData]; when you call reloadData for a tableview it should update the tableviews.
Since you are using tab bar controllers when each view is pushed to the stack it stays there you need to detect if view is appear again and call reloadData method.
in completed.m add this:
-(void) viewWillAppear:(BOOL)animated{
[self.tableview reloadData];
}
in incomplete.m detect if NSFileManager is saved data then call [self.tableview reloadData];
there are also other ways to solve this problem, you can communicate between viewcontrollers via Delegate or NSNotifications pattern. So lets say you saved the data you call your delegate or NSNotification method and receiver view controller runs your desired method. In your case refreshes the tableview
How do I set up a simple delegate to communicate between two view controllers?
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_nsnotificationcenter/

NSMutableDictionary updates not working in UItableview

I'm using *NSMutableDictionary articleDictionary as a datasource for tableview.I have different tab in tableview page, each tab i'm using different xml file [https://domain.com/api/categories/3/user/xml ] . I received different set of values in (void)viewDidUnload area but not reflect in table view .Tableview still showing old values.Please help me . Thanks
self.articleDictionary = [NSDictionary dictionaryWithContentsOfURL: [NSURL URLWithString:urlString]];
Try triggering reload of the table view when you are done with parsing. That is reload the table from the same method where you update your NSMutableDictionary.
Call [self.tableView reloadData];.
You have to tell the tableview to update. The tableview can't guess that your value has been updated ;)
Something like that should help
// Update your files normally...
// ...
// Trigger the reload
[self.tableView reloadData];
You can put ample debugging stuff into your table view controller class.
Here are some pointers:
When you call reloaddata, do you observe how many times tableView:cellForRowAtIndexPath get called? Do you get proper index values there?
Have you updated your tableView:numberOfRowsInSection: to reflect change in data source? ie the latest NSMutableDictionary count?
In your tableView:cellForRowAtIndexPath, put NSLog about NSMutableDictionary to see if it reflects the latest status?
I resolved the issue.
I changed my UiviewController superclass to UITableViewController. Thanks for your helps.

Reload UITableView with a different number of rows

I've got a UITableView set up and working, and now I have a little issue. I need to reload the tableView's data whenever the view loads, and one thing which I can't get working is attempting to reload the data, when there was no data in the table view to begin with.
Basically the list is of variable length, and the contents are loaded from a file, what I want to know is, is there I way I can force the table to reload and not have it ignore the datasource methods, as is what happens whenever [tableView reloadData] is called.
[tableView reloadData] relies entirely on the data source methods! So, the only way you would see your data source methods beind ignored would be that you have not set the data source (and perhaps delegate) of your table view to be the object you want to be the data source. You can set these through Interface Builder, or programatically, e.g.:
tableView.dataSource = self;
tableView.delegate = self;
whenever the view loads
Where do you call reloadData? Do you call it in -viewDidLoad method? If yes it is probably incorrect, because -viewDidLoad is called only once - when view of the corresponding UIViewController is created (on the first usage). Maybe you should take a look at -viewWillAppear which is called(assuming correct usage of UIViewController) whenever view is going to be displayed.
The other possible reason is that if you do use -viewWillAppear (or -viewDidAppear) it is not triggered at all. This can happen if you use a custom UIViewController hierarchy. In that case you must call it with your own hands (there are exceptions - UINavigationController for example does this for you, but simple [someView addSubview:myController.view] doesn't).
Also please check if delegates are set correctly and tableView is not equal to nil (as you know messages to nil are just ignored).
datasource methods aren't ignored when u call [tableView reloadData];
Did u set the IBOUTLET and the sources right?

Resources