botptr
botptr

Reputation: 977

respondsToSelector always fails

I am in the process of writing my own gridview implementation( Based of the tableview pattern). I have followed a datasource model similar to table view, but when I check if the data source responds to the message the call always fails.

I have tried putting in break points and following execution, I even tried missing out the call to respondsToSelector. Nothing I try seems to work. What am I missing here? Thanks in advance

GridView.h

    ...
    @protocol GridViewDataSource 
    - (NSInteger) numberOfRowsForGridView:(GridView *)gridView;
    - (NSInteger) numberOfColumnsForGridView:(GridView *)gridView;

    - (GridViewCell *) cellForRow:(NSInteger) row column:(NSInteger )column;
    @end 

GridView.m

    ...
    #pragma mark datasource methods
    -(NSInteger)numberOfRowsForGridView
    {
        if( [dataSource respondsToSelector:@selector(numberOfRowsForGridView:)] )
           return [dataSource numberOfRowsForGridView:self];
       // NSLog(@"Failed, dataSource does not implement properly");
        return 0;
    }

    -(NSInteger)numberOfColumnsForGridView
    {
        if( [dataSource respondsToSelector:@selector(numberOfColumnsForGridView:)] )
            return [dataSource numberOfColumnsForGridView:self];
        return 0;
    }

    -(GridViewCell *)cellForRow:(NSInteger)row column:(NSInteger)column
    {
        if( [dataSource respondsToSelector:@selector(cellForRow:column:)])
            return [dataSource cellForRow:row column:column];
        return nil;
    }

GridViewAppDelegate.h

    ...
    @interface GridViewAppDelegate : NSObject <UIApplicationDelegate, GridViewDataSource>
    ...

GridViewAppDelegate.m

    #pragma mark datasource methods
    -(NSInteger)numberOfRowsForGridView:(GridView *)gridView
    {
        return 1;
    }

    -(NSInteger)numberOfColumnsForGridView:(GridView *)gridView
    {
        return 1;
    }

    -(GridViewCell *)cellForRow:(NSInteger)row column:(NSInteger)column
    {
        CGRect frame = [view bounds];


        GridViewCell *cell = [[GridViewCell alloc]initWithFrame:frame];
        return cell;
    }

Upvotes: 1

Views: 1234

Answers (2)

PeyloW
PeyloW

Reputation: 36752

Is your dataSource object nil?

Any message sent to nil will return NO for boolean results (0 for numbers, and nil for objects as well).

Upvotes: 1

Akshay
Akshay

Reputation: 5765

Have you checked whether dataSource is non-nil?

Upvotes: 0

Related Questions