respondsToSelector总是失败

问题描述:

我正在编写我自己的gridview实现过程(基于tableview模式)。我遵循类似于表视图的数据源模型,但是当我检查数据源是否响应消息时,调用总是失败。respondsToSelector总是失败

我已经试过把断点放在下面执行,我甚至试着错过了对respondsToSelector的调用。没有我尝试似乎工作。我在这里错过了什么?在此先感谢

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; 
    } 

是您dataSource对象nil

发送到nil将返回NO布尔结果任何消息(0数字,和nil的对象以及)

+0

Arg,我不敢相信我自己。我只是在init函数中加载视图的数据,而不是在数据源被重置时。非常感谢。 – botptr

你检查了dataSource是否为非零?