首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >respondsToSelector总是失败

respondsToSelector总是失败
EN

Stack Overflow用户
提问于 2011-08-11 13:09:42
回答 2查看 1.2K关注 0票数 1

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

我试着输入断点,在执行之后,我甚至尝试忽略了对respondsToSelector的调用。我试过的东西似乎都没有用。我在这里错过了什么?提前感谢

GridView.h

代码语言:javascript
复制
    ...
    @protocol GridViewDataSource 
    - (NSInteger) numberOfRowsForGridView:(GridView *)gridView;
    - (NSInteger) numberOfColumnsForGridView:(GridView *)gridView;

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

GridView.m

代码语言:javascript
复制
    ...
    #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

代码语言:javascript
复制
    ...
    @interface GridViewAppDelegate : NSObject <UIApplicationDelegate, GridViewDataSource>
    ...

GridViewAppDelegate.m

代码语言:javascript
复制
    #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;
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-11 13:13:41

是您的dataSource对象nil

发送给nil的任何消息都将返回布尔结果的NO (数字为0,对象也为nil )。

票数 1
EN

Stack Overflow用户

发布于 2011-08-11 13:15:18

你检查过dataSource是否不是零了吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7026438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档