我有一个 iPad 应用程序,使用 Storyboards、Tab Bar 和 XCode 4.5。我的一个场景在左上象限有一个 UITableView,在右上象限有标签和 UITextBoxes。
当我单击第二个选项卡时,我将进入上述场景。这是代码:
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"MY_CELL"];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 4;
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return 4;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath; {
Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];
cell.label.text = [NSString stringWithFormat:@"%d",indexPath.item];
return cell;
}
这是接口(interface)定义:
@interface ClientViewController : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource> {
}
这是错误:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x7d925b0'
如您所见,我对 numberOfSectionsInCollectionView 使用了相同的代码。我研究过 SO 和 Google,但没有发现任何适用的内容。
我做错了什么?
最佳答案
有两个代表需要设置。一种是委托(delegate)
,一种是数据源
。一般来说,两者都应该设置为指向您的 View Controller (即,在大多数情况下,self
)。
关于objective-c - 返回整数作为节中项目数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13443489/