objective-c - 返回整数作为节中项目数时出错

标签 objective-c ios xcode4.5 uicollectionview uicollectionviewcell

我有一个 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/

相关文章:

iphone - 检测 iPhone 相机方向

ios - 如何从Objective C中的另一个类正确访问变量

javascript - iOS - 月出和月落的月球计算

interface-builder - 在 IB 中设置自定义类在 Xcode 4.5 中不起作用吗?

ios - sqlite数据库只更新1条记录

ios - 我们怎样才能改变我们真正的 iPhone 设备的位置?

ios - Xcode 错误 : library not found for -lGoogleToolboxForMac

objective-c - ObjectiveZlib 未解压

ios - 将 UIColor 存储在数组中

c++ - XCode 4.5 'tr1/type_traits' 找不到文件