respondsToSelector总是失败

时间:2011-08-11 13:09:43

标签: iphone objective-c ios

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

我试过放入断点并执行后,我甚至试图错过了对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;
    }

2 个答案:

答案 0 :(得分:1)

您的dataSource对象nil

发送给nil的任何邮件都会返回NO以获取布尔结果(数字为0,对象也为nil

答案 1 :(得分:0)

您是否检查过dataSource是否为非?