218

I am trying to scroll to the bottom of a UITableView after it is done performing [self.tableView reloadData].

I originally had

 [self.tableView reloadData]
 NSIndexPath* indexPath = [NSIndexPath indexPathForRow: ([self.tableView numberOfRowsInSection:([self.tableView numberOfSections]-1)]-1) inSection: ([self.tableView numberOfSections]-1)];

[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

But then I read that reloadData is asynchronous, so the scrolling doesn't happen since the self.tableView, [self.tableView numberOfSections] and [self.tableView numberOfRowsinSection are all 0.

It's weird that I am using:

[self.tableView reloadData];
NSLog(@"Number of Sections %d", [self.tableView numberOfSections]);
NSLog(@"Number of Rows %d", [self.tableView numberOfRowsInSection:([self.tableView numberOfSections]-1)]-1);

In the console it returns Sections = 1, Row = -1;

When I do the exact same NSLogs in cellForRowAtIndexPath I get Sections = 1 and Row = 8; (8 is right)

4

18 Answers 18

318

The reload happens during the next layout pass, which normally happens when you return control to the run loop (after, say, your button action or whatever returns).

So one way to run something after the table view reloads is simply to force the table view to perform layout immediately:

[self.tableView reloadData];
[self.tableView layoutIfNeeded];
 NSIndexPath* indexPath = [NSIndexPath indexPathForRow: ([self.tableView numberOfRowsInSection:([self.tableView numberOfSections]-1)]-1) inSection: ([self.tableView numberOfSections]-1)];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

Another way is to schedule your after-layout code to run later using dispatch_async:

[self.tableView reloadData];

dispatch_async(dispatch_get_main_queue(), ^{
     NSIndexPath* indexPath = [NSIndexPath indexPathForRow: ([self.tableView numberOfRowsInSection:([self.tableView numberOfSections]-1)]-1) inSection:([self.tableView numberOfSections]-1)];

    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
});

UPDATE

Upon further investigation, I find that the table view sends tableView:numberOfSections: and tableView:numberOfRowsInSection: to its data source before returning from reloadData. If the delegate implements tableView:heightForRowAtIndexPath:, the table view also sends that (for each row) before returning from reloadData.

However, the table view does not send tableView:cellForRowAtIndexPath: or tableView:headerViewForSection until the layout phase, which happens by default when you return control to the run loop.

I also find that in a tiny test program, the code in your question properly scrolls to the bottom of the table view, without me doing anything special (like sending layoutIfNeeded or using dispatch_async).

16
  • 3
    @rob, depending on how big your table data source is, you can animate going to the bottom of the tableview in the same run loop. If you try your test code with a huge table, your trick of using GCD to delay scrolling until the next run loop will work, whereas immediately scrolling will fail. But anyways, thanks for this trick!!
    – Mr. T
    May 29, 2013 at 0:56
  • 9
    Method 2 did not work for me for some unknown reason, but chose the first method instead. Aug 20, 2014 at 21:00
  • 5
    the dispatch_async(dispatch_get_main_queue()) method is not guaranteed to work. I'm seeing non-deterministic behavior with it, in which sometimes the system has completed the layoutSubviews and the cell rendering before the completion block, and sometimes after. I'll post an answer that worked for me below. Sep 22, 2016 at 20:45
  • 3
    Agree with dispatch_async(dispatch_get_main_queue()) not always working. Seeing random results here.
    – Vojto
    Jan 26, 2017 at 7:02
  • 1
    The main thread runs an NSRunLoop. A run loop has different phases, and you can schedule a callback for a specific phase (using a CFRunLoopObserver). UIKit schedules layout to happen during a later phase, after your event handler returns.
    – rob mayoff
    Dec 30, 2017 at 18:02
116

Swift:

extension UITableView {
    func reloadData(completion:@escaping ()->()) {
        UIView.animate(withDuration: 0, animations: reloadData)
            { _ in completion() }
    } 
}

// ...somewhere later...

tableView.reloadData {
    print("done")
}

Objective-C:

[UIView animateWithDuration:0 animations:^{
    [myTableView reloadData];
} completion:^(BOOL finished) {
    //Do something after that...
}];
4
  • 19
    That is equivalent to dispatching something on the main thread in the "near future". It's likely you're just seeing the table view render the objects before the main thread deques the completion block. It's not advised to do this kind of hack in the first place, but in any case, you should use dispatch_after if you're going to attempt this.
    – seo
    May 16, 2014 at 23:19
  • 1
    The Rob's solution is good but doesn't work if there are no rows in the tableview. Aviel's solution has the advantage to work even when the table contains no lines but only sections.
    – Chrstpsln
    Dec 22, 2014 at 10:41
  • @Christophe As of now, I was able to use Rob's update in a table view without any rows by overriding in my Mock view controller the tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int method and inserting in my override whatever I wanted to notify that the reload had finished.
    – Gobe
    Feb 24, 2016 at 21:57
  • This is great and works with collection views as well Jun 2, 2021 at 16:19
64

As of Xcode 8.2.1, iOS 10, and Swift 3,

You can determine the end of tableView.reloadData() easily by using a CATransaction block:

CATransaction.begin()
CATransaction.setCompletionBlock({
    print("reload completed")
    // Your completion code here
})
print("reloading")
tableView.reloadData()
CATransaction.commit()

The above also works for determining the end of UICollectionView's reloadData() and UIPickerView's reloadAllComponents().

7
  • 👍 I also works if you're performing custom reloading, like manually inserting, deleting or moving rows in table view, within beginUpdates and endUpdates calls.
    – Darrarski
    May 11, 2018 at 9:42
  • I believe this is actually the modern solution. indeed it's the common pattern in iOS, example ... stackoverflow.com/a/47536770/294884
    – Fattie
    Aug 16, 2018 at 3:00
  • I tried this. I got a very odd behavior. My tableview correctly shows two headerViews. Inside the setCompletionBlock my numberOfSections shows 2 ...so far so good. Yet if inside setCompletionBlock I do tableView.headerView(forSection: 1) it returns nil!!! hence I think this block is either happening before the reload or captures something before or I'm doing something wrong. FYI I did try Tyler's answer and that worked! @Fattie
    – mfaani
    Aug 17, 2018 at 13:03
  • I am using this it for scrolling to the top of the table once the the table data has reloaded. It works great in most cases but has an offset if the height of the top row is different between before and after the reload. This would seem to correlate with rob mayoff's findings.
    – Leon
    Sep 23, 2020 at 15:46
  • This was very helpful thanks! I had an issue where calling reloadData() on my tableview would sometimes trigger the tableview's scrollViewDidScroll() method. I was able to block the scrollViewDidScroll() from being called until the completion block was finished.
    – JTODR
    Oct 13, 2020 at 13:47
44
+50

The dispatch_async(dispatch_get_main_queue()) method above is not guaranteed to work. I'm seeing non-deterministic behavior with it, in which sometimes the system has completed the layoutSubviews and the cell rendering before the completion block, and sometimes after.

Here's a solution that works 100% for me, on iOS 10. It requires the ability to instantiate the UITableView or UICollectionView as a custom subclass. Here's the UICollectionView solution, but it's exactly the same for UITableView:

CustomCollectionView.h:

#import <UIKit/UIKit.h>

@interface CustomCollectionView: UICollectionView

- (void)reloadDataWithCompletion:(void (^)(void))completionBlock;

@end

CustomCollectionView.m:

#import "CustomCollectionView.h"

@interface CustomCollectionView ()

@property (nonatomic, copy) void (^reloadDataCompletionBlock)(void);

@end

@implementation CustomCollectionView

- (void)reloadDataWithCompletion:(void (^)(void))completionBlock
{
    self.reloadDataCompletionBlock = completionBlock;
    [self reloadData];
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    if (self.reloadDataCompletionBlock) {
        self.reloadDataCompletionBlock();
        self.reloadDataCompletionBlock = nil;
    }
}

@end

Example usage:

[self.collectionView reloadDataWithCompletion:^{
    // reloadData is guaranteed to have completed
}];

See here for a Swift version of this answer

8
  • This is the only correct way. Added it to my project because I needed the final frames of some cells for animation purposes. I also added and edit for Swift. Hope you don't mind 😉
    – Jon Vogel
    Apr 21, 2017 at 0:28
  • 3
    After you call the block in layoutSubviews it should be set to nil as subsequent calls to layoutSubviews, not necessarily due to reloadData being called, will result in the block being executed as there is a strong reference being held, which is not the desired behaviour. Jun 16, 2017 at 14:00
  • why can't I use this for UITableView? it's showing no visible interface. I imported the header file also but still same
    – Julfikar
    Nov 23, 2017 at 3:05
  • 2
    An addendum to this answer is that it's possible to clobber the existing callback if there's just one, meaning multiple callers will have a race-condition. The solution is to make reloadDataCompletionBlock an array of blocks and iterate over them on execution and empty the array after that. Nov 24, 2017 at 7:24
  • 1) isn't this equivalent to Rob's first answer ie to use layoutIfNeeded? 2) why did you mention iOS 10, does it not work on iOS 9?!
    – mfaani
    Dec 31, 2017 at 1:15
34

I had the same issues as Tyler Sheaffer.

I implemented his solution in Swift and it solved my problems.

Swift 3.0:

final class UITableViewWithReloadCompletion: UITableView {
  private var reloadDataCompletionBlock: (() -> Void)?

  override func layoutSubviews() {
    super.layoutSubviews()

    reloadDataCompletionBlock?()
    reloadDataCompletionBlock = nil
  }


  func reloadDataWithCompletion(completion: @escaping () -> Void) {
    reloadDataCompletionBlock = completion
    self.reloadData()
  }
}

Swift 2:

class UITableViewWithReloadCompletion: UITableView {

  var reloadDataCompletionBlock: (() -> Void)?

  override func layoutSubviews() {
    super.layoutSubviews()

    self.reloadDataCompletionBlock?()
    self.reloadDataCompletionBlock = nil
  }

  func reloadDataWithCompletion(completion:() -> Void) {
      reloadDataCompletionBlock = completion
      self.reloadData()
  }
}

Example Usage:

tableView.reloadDataWithCompletion() {
 // reloadData is guaranteed to have completed
}
4
  • 1
    nice! small nit-pick, you can remove the if let by saying reloadDataCompletionBlock?() which will call iff not nil 💥 Dec 17, 2016 at 6:42
  • No luck with this one in my situation on ios9
    – Matjan
    Feb 5, 2017 at 20:20
  • self.reloadDataCompletionBlock? { completion() } should have been self.reloadDataCompletionBlock?()
    – emem
    May 26, 2017 at 0:46
  • How do I handle resize of table view height? I was previously calling tableView.beginUpdates() tableView.layoutIfNeeded() tableView.endUpdates()
    – Dracula
    Nov 15, 2019 at 5:17
12

And a UICollectionView version, based on kolaworld's answer.

It needs testing. It works so far on iOS 9.2 and Xcode 9.2 beta 2, with scrolling a collectionView to an index, as a closure.

extension UICollectionView
{
    /// Calls reloadsData() on self, and ensures that the given closure is
    /// called after reloadData() has been completed.
    ///
    /// Discussion: reloadData() appears to be asynchronous. i.e. the
    /// reloading actually happens during the next layout pass. So, doing
    /// things like scrolling the collectionView immediately after a
    /// call to reloadData() can cause trouble.
    ///
    /// This method uses CATransaction to schedule the closure.

    func reloadDataThenPerform(_ closure: @escaping (() -> Void))
    {
        CATransaction.begin()
            CATransaction.setCompletionBlock(closure)
            self.reloadData()
        CATransaction.commit()
    }
}

Usage:

myCollectionView.reloadDataThenPerform {
    myCollectionView.scrollToItem(at: indexPath,
            at: .centeredVertically,
            animated: true)
}
6

When [tableView reloadData] returns, the internal data structures behind the tableView have been updated. Therefore, when the method completes you can safely scroll to the bottom. I verified this in my own app. The widely accepted answer by rob mayoff, while also confusing in terminology, acknowledges the same in his last update.

If your tableView isn't scrolling to the bottom you may have an issue in other code you haven't posted. Perhaps you are changing data after scrolling is complete and you're not reloading and/or scrolling to the bottom then?

Add some logging as follows to verify that the table data is correct after reloadData. I have the following code in a sample app and it works perfectly.

// Change the data source

NSLog(@"Before reload / sections = %d, last row = %d",
      [self.tableView numberOfSections],
      [self.tableView numberOfRowsInSection:[self.tableView numberOfSections]-1]);

[self.tableView reloadData];

NSLog(@"After reload / sections = %d, last row = %d",
      [self.tableView numberOfSections],
      [self.tableView numberOfRowsInSection:[self.tableView numberOfSections]-1]);

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.tableView numberOfRowsInSection:[self.tableView numberOfSections]-1]-1
                                                          inSection:[self.tableView numberOfSections] - 1]
                      atScrollPosition:UITableViewScrollPositionBottom
                              animated:YES];
10
  • I updated my questions. Do you know why my NSLogs would output like this?
    – Alan
    Apr 18, 2013 at 14:42
  • 8
    reloadData is not synchronous. It used to be - see this answer: stackoverflow.com/a/16071589/193896
    – bendytree
    May 24, 2013 at 0:12
  • 1
    It is synchronous. It's very easy to test and see this with a sample app. You linked to @rob's answer in this question. If you read his update at the bottom, he has verified this as well. Perhaps you are talking about the visual layout changes. It is true that the tableView is not visibly updated synchronously but the data is. That's why the values the OP needs are correct immediately after reloadData returns.
    – XJones
    May 24, 2013 at 0:42
  • 1
    You may be confused about what is expected to happen in reloadData. Use my test case in viewWillAppear accept for the scrollToRowAtIndexPath: line b/c that is meaningless if the tableView isn't displayed. You will see that reloadData did update the data cached in the tableView instance and that reloadData is synchronous. If you are referring to other tableView delegate methods called when the tableView is being layout out those won't get called if the tableView is not displayed. If I am misunderstanding your scenario please explain.
    – XJones
    May 23, 2014 at 21:17
  • 4
    What fun times. It's 2014, and there are arguments over whether some method is synchronous and asynchronous or not. Feels like guesswork. All implementation detail is completely opaque behind that method name. Isn't programming great?
    – fatuhoku
    Jul 12, 2014 at 12:49
5

I use this trick, pretty sure I already posted it to a duplicate of this question:

-(void)tableViewDidLoadRows:(UITableView *)tableView{
    // do something after loading, e.g. select a cell.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // trick to detect when table view has finished loading.
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(tableViewDidLoadRows:) object:tableView];
    [self performSelector:@selector(tableViewDidLoadRows:) withObject:tableView afterDelay:0];

    // specific to your controller
    return self.objects.count;
}
2
  • @Fattie it's unclear if you mean it as a positive comment or a negative comment. But I saw you've commented another answer as "this seems to be the best solution!", so I guess that relatively speaking, you do not consider this solution to be the best.
    – Cœur
    Jul 11, 2018 at 3:59
  • 1
    Relying on a side affect of a fake animation? No way is that a good idea. Learn perform selector or GCD and do it properly. Btw there is now a table loaded method you could just use that if you don’t mind using a private protocol which is prob fine because it’s the framework calling your code rather than other way around.
    – malhal
    Jul 12, 2018 at 21:53
4

Actually this one solved my problem:

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

NSSet *visibleSections = [NSSet setWithArray:[[tableView indexPathsForVisibleRows] valueForKey:@"section"]];
if (visibleSections) {
    // hide the activityIndicator/Loader
}}
1

Details

  • Xcode Version 10.2.1 (10E1001), Swift 5

Solution

import UIKit

// MARK: - UITableView reloading functions

protocol ReloadCompletable: class { func reloadData() }

extension ReloadCompletable {
    func run(transaction closure: (() -> Void)?, completion: (() -> Void)?) {
        guard let closure = closure else { return }
        CATransaction.begin()
        CATransaction.setCompletionBlock(completion)
        closure()
        CATransaction.commit()
    }

    func run(transaction closure: (() -> Void)?, completion: ((Self) -> Void)?) {
        run(transaction: closure) { [weak self] in
            guard let self = self else { return }
            completion?(self)
        }
    }

    func reloadData(completion closure: ((Self) -> Void)?) {
        run(transaction: { [weak self] in self?.reloadData() }, completion: closure)
    }
}

// MARK: - UITableView reloading functions

extension ReloadCompletable where Self: UITableView {
    func reloadRows(at indexPaths: [IndexPath], with animation: UITableView.RowAnimation, completion closure: ((Self) -> Void)?) {
        run(transaction: { [weak self] in self?.reloadRows(at: indexPaths, with: animation) }, completion: closure)
    }

    func reloadSections(_ sections: IndexSet, with animation: UITableView.RowAnimation, completion closure: ((Self) -> Void)?) {
        run(transaction: { [weak self] in self?.reloadSections(sections, with: animation) }, completion: closure)
    }
}

// MARK: - UICollectionView reloading functions

extension ReloadCompletable where Self: UICollectionView {

    func reloadSections(_ sections: IndexSet, completion closure: ((Self) -> Void)?) {
        run(transaction: { [weak self] in self?.reloadSections(sections) }, completion: closure)
    }

    func reloadItems(at indexPaths: [IndexPath], completion closure: ((Self) -> Void)?) {
        run(transaction: { [weak self] in self?.reloadItems(at: indexPaths) }, completion: closure)
    }
}

Usage

UITableView

// Activate
extension UITableView: ReloadCompletable { }

// ......
let tableView = UICollectionView()

// reload data
tableView.reloadData { tableView in print(collectionView) }

// or
tableView.reloadRows(at: indexPathsToReload, with: rowAnimation) { tableView in print(tableView) }

// or
tableView.reloadSections(IndexSet(integer: 0), with: rowAnimation) { _tableView in print(tableView) }

UICollectionView

// Activate
extension UICollectionView: ReloadCompletable { }

// ......
let collectionView = UICollectionView()

// reload data
collectionView.reloadData { collectionView in print(collectionView) }

// or
collectionView.reloadItems(at: indexPathsToReload) { collectionView in print(collectionView) }

// or
collectionView.reloadSections(IndexSet(integer: 0)) { collectionView in print(collectionView) }

Full sample

Do not forget to add the solution code here

import UIKit

class ViewController: UIViewController {

    private weak var navigationBar: UINavigationBar?
    private weak var tableView: UITableView?

    override func viewDidLoad() {
        super.viewDidLoad()
        setupNavigationItem()
        setupTableView()
    }
}
// MARK: - Activate UITableView reloadData with completion functions

extension UITableView: ReloadCompletable { }

// MARK: - Setup(init) subviews

extension ViewController {

    private func setupTableView() {
        guard let navigationBar = navigationBar else { return }
        let tableView = UITableView()
        view.addSubview(tableView)
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.topAnchor.constraint(equalTo: navigationBar.bottomAnchor).isActive = true
        tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        tableView.dataSource = self
        self.tableView = tableView
    }

    private func setupNavigationItem() {
        let navigationBar = UINavigationBar()
        view.addSubview(navigationBar)
        self.navigationBar = navigationBar
        navigationBar.translatesAutoresizingMaskIntoConstraints = false
        navigationBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        navigationBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        navigationBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        let navigationItem = UINavigationItem()
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "all", style: .plain, target: self, action: #selector(reloadAllCellsButtonTouchedUpInside(source:)))
        let buttons: [UIBarButtonItem] = [
                                            .init(title: "row", style: .plain, target: self,
                                                  action: #selector(reloadRowButtonTouchedUpInside(source:))),
                                            .init(title: "section", style: .plain, target: self,
                                                  action: #selector(reloadSectionButtonTouchedUpInside(source:)))
                                            ]
        navigationItem.leftBarButtonItems = buttons
        navigationBar.items = [navigationItem]
    }
}

// MARK: - Buttons actions

extension ViewController {

    @objc func reloadAllCellsButtonTouchedUpInside(source: UIBarButtonItem) {
        let elementsName = "Data"
        print("-- Reloading \(elementsName) started")
        tableView?.reloadData { taleView in
            print("-- Reloading \(elementsName) stopped \(taleView)")
        }
    }

    private var randomRowAnimation: UITableView.RowAnimation {
        return UITableView.RowAnimation(rawValue: (0...6).randomElement() ?? 0) ?? UITableView.RowAnimation.automatic
    }

    @objc func reloadRowButtonTouchedUpInside(source: UIBarButtonItem) {
        guard let tableView = tableView else { return }
        let elementsName = "Rows"
        print("-- Reloading \(elementsName) started")
        let indexPathToReload = tableView.indexPathsForVisibleRows?.randomElement() ?? IndexPath(row: 0, section: 0)
        tableView.reloadRows(at: [indexPathToReload], with: randomRowAnimation) { _tableView in
            //print("-- \(taleView)")
            print("-- Reloading \(elementsName) stopped in \(_tableView)")
        }
    }

    @objc func reloadSectionButtonTouchedUpInside(source: UIBarButtonItem) {
        guard let tableView = tableView else { return }
        let elementsName = "Sections"
        print("-- Reloading \(elementsName) started")
        tableView.reloadSections(IndexSet(integer: 0), with: randomRowAnimation) { _tableView in
            //print("-- \(taleView)")
            print("-- Reloading \(elementsName) stopped in \(_tableView)")
        }
    }
}

extension ViewController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int { return 1 }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 20 }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = "\(Date())"
        return cell
    }
}

Results

enter image description here

1

I ended up using a variation of Shawn's solution:

Create a custom UITableView class with a delegate:

protocol CustomTableViewDelegate {
    func CustomTableViewDidLayoutSubviews()
}

class CustomTableView: UITableView {

    var customDelegate: CustomTableViewDelegate?

    override func layoutSubviews() {
        super.layoutSubviews()
        self.customDelegate?.CustomTableViewDidLayoutSubviews()
    }
}

Then in my code, I use

class SomeClass: UIViewController, CustomTableViewDelegate {

    @IBOutlet weak var myTableView: CustomTableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.myTableView.customDelegate = self
    }

    func CustomTableViewDidLayoutSubviews() {
        print("didlayoutsubviews")
        // Do other cool things here!!
    }
}

Also make sure you set your table view to CustomTableView in Interface Builder:

Enter image description here

2
  • this works but the problem is the method gets hit every time its done a loading a single Cell, NOT THE WHOLE TABLE VIEW RELOAD, so clearly this answer isn't in respect to the question asked.
    – Yash Bedi
    Aug 24, 2018 at 11:03
  • True, it gets called more than once, but not on every cell. So you could listen to the first delegate and ignore the rest until you call reloadData again.
    – Sam
    Nov 7, 2018 at 20:50
1

Try this way. It will work.

[tblViewTerms performSelectorOnMainThread:@selector(dataLoadDoneWithLastTermIndex:) withObject:lastTermIndex waitUntilDone:YES];waitUntilDone:YES];

@interface UITableView (TableViewCompletion)

-(void)dataLoadDoneWithLastTermIndex:(NSNumber*)lastTermIndex;

@end

@implementation UITableView(TableViewCompletion)

-(void)dataLoadDoneWithLastTermIndex:(NSNumber*)lastTermIndex
{
    NSLog(@"dataLoadDone");

NSIndexPath* indexPath = [NSIndexPath indexPathForRow: [lastTermIndex integerValue] inSection: 0];

[self selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];

}
@end

I will execute when table is completely loaded.

Another solution is you can subclass UITableView.

0

If you reload data when viewDidLoad, you can put your code into the viewDidLayoutSubviews method. But you should be careful that viewDidLayoutSubviews may be called for multiple times.

0

I've regularly encountered the same issue and it happened even with only a couple of rows in the table (and I ended up adding some of the contrived workarounds others have mentioned).

However, the last note in Rob's answer (saying that the posted code actually works fine for him) got me thinking. I noticed that in my particular case the problem only happened when the reload call was issued from within an observeValueForKeyPath() method.

The solution was to simply wrap the entire logic in dispatch_async(dispatch_get_main_queue(), ^{...}) inside the observeValueForKeyPath() method.

Note: I did not test this solution with very large datasets.

-1

Try this:

tableView.backgroundColor = .black

tableView.reloadData()

DispatchQueue.main.async(execute: {

    tableView.backgroundColor = .green

})

The tableView color will changed from black to green only after the reloadData() function completes.

-1

Creating a reusable extension of CATransaction:

public extension CATransaction {
    static func perform(method: () -> Void, completion: @escaping () -> Void) {
        begin()
        setCompletionBlock {
            completion()
        }
        method()
        commit()
    }
}

Now creating an extension of UITableView that would use CATransaction's extension method:

public extension UITableView {
    func reloadData(completion: @escaping (() -> Void)) {
       CATransaction.perform(method: {
           reloadData()
       }, completion: completion)
    }
}

Usage:

tableView.reloadData(completion: {
    //Do the stuff
})
-2

You can use it for do something after reload data:

[UIView animateWithDuration:0 animations:^{
    [self.contentTableView reloadData];
} completion:^(BOOL finished) {
    _isUnderwritingUpdate = NO;
}];
-20

Try setting delays:

[_tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.2];
[_activityIndicator performSelector:@selector(stopAnimating) withObject:nil afterDelay:0.2];
1
  • 15
    This is dangerous. What if it takes longer to reload than your delay?
    – rob
    Aug 6, 2014 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.