我正在编写自己的网格视图实现(基于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;
}发布于 2011-08-11 13:13:41
是您的dataSource对象nil
发送给nil的任何消息都将返回布尔结果的NO (数字为0,对象也为nil )。
发布于 2011-08-11 13:15:18
你检查过dataSource是否不是零了吗?
https://stackoverflow.com/questions/7026438
复制相似问题