I have searched SO and have yet to come to a definitive answer.
I have an app similar to Facebook and Instagram, where once you get to the bottom of the UITableView it calls a web service and loads another 25 rows of data.
The problem I facing is that on loading the new 25 pieces, I am calling reloadData and this causes unpleasant UI flashing.
What's the proper way of dynamically adding the new data to the UITableView as they scroll without the flashing (aka not calling reloadData?
After you add the new row items to your data source you need to use [tableView beginUpdates]; and [tableView endUpdates]; not reloadData.
NSMutableArray *indexPathsToAdd = [NSMutableArray new];
for (Object *object in currentObjects)
{
if (![newObjects containsObject:object]) {
int row = [currentObjects indexOfObject:object];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
[indexPathsToAdd addObject:indexPath];
}
}
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:indexPathsToAdd withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];
In one of the views there is a UITableView which is getting updated rather often.
Tracking the changes are done in a classic way using "reloadRowsAtIndexPaths"
-(void)refreshCells:(NSArray *)changedCells
{
NSLog(#"refreshCells %i",[changedCells count]);
[TableView beginUpdates];
[TableView reloadRowsAtIndexPaths:changedCells withRowAnimation:UITableViewRowAnimationBottom];
[TableView endUpdates];
}
Question: How can I preserve the user's last selected Cell. the cell position may change after each update refreshCells?
You can save the current selection with
NSIndexPath *selectedRow = [self.tableView indexPathForSelectedRow];
before the reload and select it again with
if (selectedRow) {
[self.tableView selectRowAtIndexPath:selectedRow animated:NO scrollPosition:UITableViewScrollPositionNone];
}
after the reload. The cell position does not change unless you call insertRowsAtIndexPaths: or deleteRowsAtIndexPaths:.
I have custom UITableView and UIImageView, UILabel in it. On some notification I need to update only UILabel not UIImageView. If I do like this:
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:idxCell inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
All content of the cell is updating and I have flicker effect for UIImageView.
I've done like this:
MyTableViewCell *cell = (MyTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:idx inSection:0]];
cell.counterLabel.text = [NSString stringWithFormat:#"%#", counter];
But I think that it is not good, because it is not corresponds to the MVC pattern. I guess I should update my Model object. I need advice, How I can do this?
You should update your NSArray or NSDictionary from where u fetch data. because when u reload row it again call the same dictionary or array and that has same value.
didSelectRowAtIndexPath at update the dictionary or array
and then reload the row.
cellForRowAtIndexPath update that NSArray or NSDictionary which fetch data for row.
May be it works
I am using XCode4.2 to develop a storyboard application
I am switching views based on a click in a UITableView after clicking the row is highlighted in blue and the new view is loaded .
when I come back to the original view the row is still highlighted in blue ... how can I disable that ?
I do this in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath];
}
That way I have the index path that was selected every time.
use deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated function to deselect rows in uitableview
call it in your viewWillAppear function
[yourTableView deselectRowAtIndexPath:selectedIndexPath animated:NO];
if you are not sure which is index you select last time you can run a loop on all the index path in your table.
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
for(int i = 0; i <= [yourArr count]; i++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[yourTableView deselectRowAtIndexPath:indexPath animated:NO];
}
}
Note: As I don't know your exact code....you need to modify above code to your needs.
A UITableViewController does this automatically. Otherwise, in your viewDidAppear for the owning controller, you need to call - (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated on the table view.
I have a custom UITableView using UITableViewCells.
Each UITableViewCell has 2 buttons. Clicking these buttons will change an image in a UIImageView within the cell.
Is it possible to refresh each cell separately to display the new image?
Any help is appreciated.
Once you have the indexPath of your cell, you can do something like:
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathOfYourCell, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
In Xcode 4.6 and higher:
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:#[indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
You can set whatever your like as animation effect, of course.
I tried just calling -[UITableView cellForRowAtIndexPath:], but that didn't work. But, the following works for me for example. I alloc and release the NSArray for tight memory management.
- (void)reloadRow0Section0 {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSArray *indexPaths = [[NSArray alloc] initWithObjects:indexPath, nil];
[self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
[indexPaths release];
}
Swift:
func updateCell(path:Int){
let indexPath = NSIndexPath(forRow: path, inSection: 1)
tableView.beginUpdates()
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) //try other animations
tableView.endUpdates()
}
reloadRowsAtIndexPaths: is fine, but still will force UITableViewDelegate methods to fire.
The simplest approach I can imagine is:
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
[self configureCell:cell forIndexPath:indexPath];
It's important to invoke your configureCell: implementation on main thread, as it wont work on non-UI thread (the same story with reloadData/reloadRowsAtIndexPaths:). Sometimes it might be helpful to add:
dispatch_async(dispatch_get_main_queue(), ^
{
[self configureCell:cell forIndexPath:indexPath];
});
It's also worth to avoid work that would be done outside of the currently visible view:
BOOL cellIsVisible = [[self.tableView indexPathsForVisibleRows] indexOfObject:indexPath] != NSNotFound;
if (cellIsVisible)
{
....
}
If you are using custom TableViewCells, the generic
[self.tableView reloadData];
does not effectively answer this question unless you leave the current view and come back. Neither does the first answer.
To successfully reload your first table view cell without switching views, use the following code:
//For iOS 5 and later
- (void)reloadTopCell {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSArray *indexPaths = [[NSArray alloc] initWithObjects:indexPath, nil];
[self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
}
Insert the following refresh method which calls to the above method so you can custom reload only the top cell (or the entire table view if you wish):
- (void)refresh:(UIRefreshControl *)refreshControl {
//call to the method which will perform the function
[self reloadTopCell];
//finish refreshing
[refreshControl endRefreshing];
}
Now that you have that sorted, inside of your viewDidLoad add the following:
//refresh table view
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(refresh:) forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:refreshControl];
You now have a custom refresh table feature that will reload the top cell. To reload the entire table, add the
[self.tableView reloadData]; to your new refresh method.
If you wish to reload the data every time you switch views, implement the method:
//ensure that it reloads the table view data when switching to this view
- (void) viewWillAppear:(BOOL)animated {
[self.tableView reloadData];
}
Swift 3 :
tableView.beginUpdates()
tableView.reloadRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
Here is a UITableView extension with Swift 5:
import UIKit
extension UITableView
{
func updateRow(row: Int, section: Int = 0)
{
let indexPath = IndexPath(row: row, section: section)
self.beginUpdates()
self.reloadRows(at: [indexPath], with: .automatic)
self.endUpdates()
}
}
Call with
self.tableView.updateRow(row: 1)
Just to update these answers slightly with the new literal syntax in iOS 6--you can use Paths = #[indexPath] for a single object, or Paths = #[indexPath1, indexPath2,...] for multiple objects.
Personally, I've found the literal syntax for arrays and dictionaries to be immensely useful and big time savers. It's just easier to read, for one thing. And it removes the need for a nil at the end of any multi-object list, which has always been a personal bugaboo. We all have our windmills to tilt with, yes? ;-)
Just thought I'd throw this into the mix. Hope it helps.
I need the upgrade cell but I want close the keyboard.
If I use
let indexPath = NSIndexPath(forRow: path, inSection: 1)
tableView.beginUpdates()
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) //try other animations
tableView.endUpdates()
the keyboard disappear