Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionView注册表类:forCellWithReuseIdentifier方法破坏UICollectionView_Ios_Objective C_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Ios UICollectionView注册表类:forCellWithReuseIdentifier方法破坏UICollectionView

Ios UICollectionView注册表类:forCellWithReuseIdentifier方法破坏UICollectionView,ios,objective-c,uicollectionview,uicollectionviewcell,Ios,Objective C,Uicollectionview,Uicollectionviewcell,registerClass:forCellWithReuseIdentifier:方法的作用是什么? 根据苹果的开发者文档,它应该 “注册用于创建新集合视图单元格的类。” 当我尝试在我的项目中使用它时,我得到了一个黑色的集合视图。当我删除它时,一切正常 #define cellId @"cellId" #import "ViewController.h" #import "Cell.h" @interface ViewController () @property (weak, nonatomi

registerClass:forCellWithReuseIdentifier:
方法的作用是什么? 根据苹果的开发者文档,它应该

“注册用于创建新集合视图单元格的类。”

当我尝试在我的项目中使用它时,我得到了一个黑色的集合视图。当我删除它时,一切正常

#define cellId @"cellId"
#import "ViewController.h"
#import "Cell.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property(strong, nonatomic)NSMutableArray * photoArray;


@end

@implementation ViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"%@",_photoArray);
    _photoArray = [[NSMutableArray alloc]initWithCapacity:0];
    [_collectionView registerClass:[Cell class] forCellWithReuseIdentifier:cellId];
    for(int i=1;i<=12;i++)
    {
        NSString * imgName = [NSString stringWithFormat:@"%d.png",i];
        UIImage *img  = [UIImage imageNamed:imgName];
        [_photoArray addObject:img];
    }
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _photoArray.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    Cell* cell = [_collectionView  dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    cell.cellImage.image = [_photoArray objectAtIndex:indexPath.row];
    return cell;
}
#定义cellId@“cellId”
#导入“ViewController.h”
#导入“Cell.h”
@界面视图控制器()
@属性(弱、非原子)IBUICollectionView*collectionView;
@属性(强,非原子)NSMutableArray*光阵列;
@结束
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
NSLog(@“%@”,_光阵列);
_photoArray=[[NSMutableArray alloc]initWithCapacity:0];
[\u collectionView注册表类:[Cell类]forCellWithReuseIdentifier:cellId];

对于(int i=1;i,如果您已经在故事板中创建了UICollectionView,连接了
数据源
委托
,并且添加了所有必需的方法:

  • numberOfItemsInSection
  • numberOfSectionsInCollectionView
    (不是必需的方法-)
  • cellForItemAtIndexPath
然后,不需要使用
registerClass
/
registerCell
方法。但是,如果需要重用视图、数据或单元格,则应使用这些方法,以便iOS可以根据需要填充UICollectionView。这也可以通过在故事板中设置原型单元格来完成(与
寄存器类
方法的原理相同

另外,如果您正在寻找有关
registerCell
的功能以及如何使用它的好解释,请查看并滚动到标题为“单元和视图重用”的底部部分。

同意并想指出,在

如果单元类是用代码编写的,则使用UICollectionView的registerClass:方法执行注册,否则使用RegisterInB


您的集合视图是在IB中创建的吗?谢谢@RazorSharp。但是,我想指出,
numberOfSectionsInCollectionView:
不是“必需的方法”。根据,
如果您不实现此方法,集合视图将使用默认值1。
@Soverit很好!我忘记了,请查看我的ed主要的一点是,你可以直接在故事板中连接一个原型单元,这与调用
registerClass:…
的作用是一样的。内存使用或单元重用与此无关。集合视图从不将不在屏幕上的单元加载到内存中,如果单元标识符没有实际注册瑞德,他让一个牢房排队的那条线可能会崩溃。完美的回答!!节省我很多时间谢谢山姆!!