gpt4 book ai didi

iphone - respondsToSelector 总是失败

转载 作者:行者123 更新时间:2023-11-29 13:46:49 25 4
gpt4 key购买 nike

我正在编写自己的 gridview 实现(基于 tableview 模式)。我遵循了类似于 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;
}

最佳答案

您的 dataSource 对象是 nil 吗?

发送到 nil 的任何消息都将返回 NO bool 结果(数字为 0,对象也为 nil) .

关于iphone - respondsToSelector 总是失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7026438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com