views:

53

answers:

3

I have separate files for the UITableViewDataSource and UITableViewDelegate

I am implementing a 'Pull Down to Refresh' feature on a UITableView and wish to send a signal from the UITableViewDataSource to the UITableViewDelegate to stop the loading indicator from turning.

I was wondering what the best way to send a signal between the UITableViewDataSource and UITableViewDelegate is, or if there is a better way to construct this since I feel like I'm breaking some abstraction barriers.

What I have in the Data Source:

if([tableView.delegate respondsToSelector: @selector(dataSourceDidFinishLoadingNewData)]){
    [tableView.delegate dataSourceDidFinishLoadingNewData];
}

but I get 'dataSourceDidFinishLoadingNewData' not found in protocol warning since, I guess, the function is not declared as a method of UITableViewDelegate

A: 

You propably can't do this while keeping things completely abstract. An alternative to calling the delegate via the tableViews delegate property would be exchanging an keeping references. But you could even avoid the need to keep references to each other object by using notifications to signal the delegate that loading has finished.

Check NSNotificationCenter for more information.

Toastor
ya, I think this is what I'll end up doing, thanks.
Ryan Bavetta
+1  A: 

Perhaps you are aware, but there are multiple open-source implementations of pull-to-refresh. You might want to look into those before making your own. Here is one (just the first one on Google): http://github.com/leah/PullToRefresh

David M.
thanks, I am using this version: http://github.com/enormego/EGOTableViewPullRefresh
Ryan Bavetta
+1  A: 

Just a side answer; When using the:

if([object respondsToSelector:@selector(someMethod)]){
    [object someMethod];
}

paradigm, you can just use this instead:

if([object respondsToSelector:@selector(someMethod)]){
    [object performSelector:@selector(someMethod)];
}

Might be a little lengthier, but it won't give you any warnings.

David Liu
good call, do you know why this is?
Ryan Bavetta
It's because performSelector only knows what method is being called at run-time. That means that during compile time, it can't detect whether a certain message is listed in the interface or not. It's kind of a double-edged sword.
David Liu