开发者

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( [da开发者_C百科taSource 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;
    }


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).


Have you checked whether dataSource is non-nil?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜