Using reloadRowsAtIndexPaths when the update of a NSFetchedResultsController is triggered - ios

I have some rows of my UITableView that get updated and then trigger the method controller:didChangeObject:atIndexPath:forChangeType:newIndexPath of the NSFetchedResultsControllerDelegate delegate.
I usually update the rows in the tableView using reloadRowsAtIndexPaths with an animation, but it happens that sometimes the method is triggered because of a change that is not visible and is that case, I don't want to reload the row with an animation.
The method doesn't give the previous state of the object. Is there a way to do it (I didn't find any method of the delegate that might do)?

If your delegate is also your TableViewController, you could just check your UITableView's data source at the IndexPath. The reloadRowsAtIndexPaths method knows the index.

Related

UITableView: Does dequed cells removed when reloadData called?

When an application calls reloadData method on UITableView, doeas previously dequed cells removed or are they still kept?
It will keep the visible cells.
If the cell is made in Storyboard it will stay there always dequeued.
Reloading the table view clears current state, including the current selection. However, if you explicitly call reloadData(), it clears this state,
any subsequent direct or indirect call to layoutSubviews() does not
trigger a reload
.
Here is link to Documentation.
reloadData dequeues new cells for the visible rows and removes the previous cells.

visibleCells are empty after reloadData/invalidateLayout - how to retrieve the visible cells at the correct time

I want to access the visibleCells in scrollViewDidEndDecelerating, but I get an empty array. The reason is that I called reloadData and invalidateLayout shortly before in viewWillLayoutSubviews and it seems that both functions (scrollViewDidEndDecelerating and viewWillLayoutSubviews) are asynchronous. Even if want to get the visible cells, right after calling reloadData I get an empty array. So it has not been finished recalculating everything.
How do I know at which time/which method there are valid visible cells? Do I have to force everything running on the main UI thread or how do I run that in sync?
Edit:
My solution is to use another approach and don't use the visible cells for this.

UITableView inform UITableViewCell about reload

Here is a short summary of the situation:
I have a TableViewController with a number of cells.
In one of the cells I have a UITextField. This cell is of a custom UITableViewCell class. This class implements the UITextFieldDelegate protocol and the method textFieldDidEndEditing:
Within the method textFieldDidEndEditing: I store the value of the TextField into CoreData.
This all works great.
My question relates to reloading the TableView:
After certain actions of the user I reload the data of the UITableView by calling [self.tableView reloadData]
If, when I call this method, the above mentioned TextField is first responder, the textFieldDidEndEditing: method will be called, causing my CoreData related code to execute.
When the TableView is being reloaded I do not want the custom TabelViewCell to do anything at all.
Is there a way, the TableViewCell can be aware of the TableView being reloaded? So I can check for this within the textFieldDidEndEditing: method?
In the meantime I have solved it in the following way:
When this TableViewCell is being created I let the ViewController store a reference to it.
Before calling [self.tableView reloadData] I first inform the Cell by setting a custom property _cell.tableWillReload = YES
I will check for this property within the textFieldDidEndEditing: method.
I was thinking maybe there is a different, more default way, for TableViewCells to know they surroundingTableView is reloading?
Try This :-
[yourTextFieldOfCell resignFirstResponder];
before calling [self.tableView reloadData];

Delete/Insert UITableViewCell not in Editing mode

What's best solution to remove cell from tableview doing this not in editing mode. Like example switching to one view to another , update number of cells (after a previous delete or insert of cell) in -viewWillAppear: method.
You could use NSFetchedResultsController. It will add/remove/update cells as it gets notifications when changes are made to NSManagedObjectContext that it was created with.
If you changed (added/deleted) the values from the dataSource array then you can simply call
[tableView reloadData];
Look at beginUpdates
Call this method if you want subsequent insertions, deletion, and selection operations (for example, cellForRowAtIndexPath: and indexPathsForVisibleRows) to be animated simultaneously.
You should not call reloadData within the group

UITableView initial row selection

Maybe I'm missing something obvious, but I'm have a hard time programmatically setting the row selection in a tableView. The goal is to simply have a tableView open with a row already selected. The problem appears to be that I have to wait until the tableView is fully loaded before I can modify the selection.
I've read various strategies such as calling reloadData for the tableView in the viewController's viewWillAppear method, then immediately calling selectRowAtIndexPath for the target row. But when I do that, I get a range exception because the tableView has zero rows at that point. The UITableViewDelegate methods (numberOfRowsInSection, etc.) don't appear to be called immediately in response to reloadData (which makes sense if the table rows are drawn "lazily").
The only way I've been able to get this to work is to call selectRowAtIndexPath after a short delay, but then you can see the tableView scroll the selected row into view.
Surely, there's a better way of doing this?
Well, you can use another strategy. You can create a hidden table view, configure how you want and than show to user. Use the tableview.hidden = YES.

Resources