我正在編寫我自己的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;
}
Arg,我不敢相信我自己。我只是在init函數中加載視圖的數據,而不是在數據源被重置時。非常感謝。 – botptr