您好我创建了一个UIViewController
,因为我在CustomCell中添加了一个UICollectionView
。然后,我为单元格创建了一个uicollectionviewcell
类。最后在storybord本身设置委托和数据源。但它显示空页。如下图所示
CODE:
- (void)viewDidLoad
{
[super viewDidLoad];
self.collectionview = _collectionview;
self.collectionview.dataSource = self;
self.collectionview.delegate = self;
speakersImages=[[NSMutableArray alloc]init];
// Do any additional setup after loading the view, typically from a nib.
imageLabels=[[NSMutableArray alloc]initWithObjects:@"RB Forum 2013",@"Agenda",@"Speakers",@"Contact",@"Map",@"Websites",@"@Responsible Biz",@"Partners",@"Sustainability",nil];
imagePaths=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"RBform.png"],[UIImage imageNamed:@"agenda.png"],[UIImage imageNamed:@"speakers.png"],[UIImage imageNamed:@"contact.png"],[UIImage imageNamed:@"map.png"],[UIImage imageNamed:@"website.png"],[UIImage imageNamed:@"twitter.png"],[UIImage imageNamed:@"partners.png"],[UIImage imageNamed:@"sustainability.png"], nil];
[self.collectionview registerClass:[NewCell class] forCellWithReuseIdentifier:@"Cell"];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return [imageLabels count];
}
//THE BELOW DELEGATE METHOD DOES NOT GET CALLED!
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// we're going to use a custom UICollectionViewCell, which will hold an image and its label
//
static NSString *CellIdentifier = @"Cell";
NewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
// make the cell's title the actual NSIndexPath value
cell.labels.text = [NSString stringWithFormat:@"%@",[imageLabels objectAtIndex:indexPath.row]];
// load the image for this cell
NSLog(@"%@",imageLabels);
//NSString *imageToLoad = [NSString stringWithFormat:@"%d.JPG", indexPath.row];
// cell.image.image = [UIImage imageNamed:imageToLoad];
cell.images.image = [imagePaths objectAtIndex:indexPath.row];
return cell;
}
答案 0 :(得分:22)
删除解决方案 [self.collectionview registerClass:[NewCell class] forCellWithReuseIdentifier:@“Cell”];
在storybord中我们不需要这个
答案 1 :(得分:1)
代码没问题,但顺序不同。在填充数组之前分配数据源和委托使得单元格的数量返回到0,因为数组尚未填充
试试这个
speakersImages=[[NSMutableArray alloc]init];
// Do any additional setup after loading the view, typically from a nib.
imageLabels=[[NSMutableArray alloc]initWithObjects:@"RB Forum 2013",@"Agenda",@"Speakers",@"Contact",@"Map",@"Websites",@"@Responsible Biz",@"Partners",@"Sustainability",nil];
imagePaths=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"RBform.png"],[UIImage imageNamed:@"agenda.png"],[UIImage imageNamed:@"speakers.png"],[UIImage imageNamed:@"contact.png"],[UIImage imageNamed:@"map.png"],[UIImage imageNamed:@"website.png"],[UIImage imageNamed:@"twitter.png"],[UIImage imageNamed:@"partners.png"],[UIImage imageNamed:@"sustainability.png"], nil];
[self.collectionview registerClass:[NewCell class] forCellWithReuseIdentifier:@"Cell"];
[self.collectionview reloadData];
不需要此行
self.collectionview = _collectionview;
self.collectionview.dataSource = self;
self.collectionview.delegate = self;
还要确保您的收藏视图通过插座连接
确保课程标题中有<UICollectioniewDatasource,UICollectionviewDelegate>
答案 2 :(得分:0)
在您的故事板中,为您的imageview
tag
例如{1}} 1,您的label
另一个tag
,例如2
提供您已经提供的cell
重用标识符,例如: “Cell
”
现在在您的故事板中右键单击collection view
并为您的viewController或文件所有者创建datasource
和delegate
。或者,您可以在viewController中使用<UICollectionViewDataSource,UICollectionViewDelegate>
并将委托和数据源设置为self。
现在在viewDidLoad
中 - (void)
viewDidLoad
{
self.collectionview.dataSource = self;
self.collectionview.delegate = self;
speakersImages=[[NSMutableArray alloc]init];
// Do any additional setup after loading the view, typically from a nib.
imageLabels=[[NSMutableArray alloc]initWithObjects:@"RB Forum 2013",@"Agenda",@"Speakers",@"Contact",@"Map",@"Websites",@"@Responsible Biz",@"Partners",@"Sustainability",nil];
imagePaths=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"RBform.png"],[UIImage imageNamed:@"agenda.png"],[UIImage imageNamed:@"speakers.png"],[UIImage imageNamed:@"contact.png"],[UIImage imageNamed:@"map.png"],[UIImage imageNamed:@"website.png"],[UIImage imageNamed:@"twitter.png"],[UIImage imageNamed:@"partners.png"],[UIImage imageNamed:@"sustainability.png"], nil];
}
集合视图中的项目数
(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger的)部分 {
return [imageLabels count];
}
创建单元格并重复使用
(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell"];
// make the cell's title the actual NSIndexPath value
UIlabel *label = (UIlabel *) [cell viewWithTag:2]
label.text = [NSString stringWithFormat:@"%@",[imageLabels objectAtIndex:indexPath.row]];
// load the image for this cell
UIImageView *imageView = (UIImageView *) [cell viewWithTag:1]
imageView.image = [imagePaths objectAtIndex:indexPath.row];
return cell;
}
答案 3 :(得分:-1)
- (void)viewDidLoad
{
[super viewDidLoad];
[self.collectionView registerNib:[UINib nibWithNibName:@"NewCell" bundle:nil] forCellWithReuseIdentifier:@"CELL"];
self.collectionview.dataSource = self;
self.collectionview.delegate = self;
self.imageLabels = [NSArray arrayWithObjects:@"RB Forum 2013",@"Agenda",@"Speakers",@"Contact",@"Map",@"Websites",@"@Responsible Biz",@"Partners",@"Sustainability",nil];
self.imagePaths=[NSArray arrayWithObjects::@"RBform.png",@"agenda.png",@"speakers.png",@"contact.png",@"map.png",@"website.png",@"twitter.png",@"partners.png",@"sustainability.png", nil];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return [self.imageLabels count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
cell.labels.text = [self.imageLabels objectAtIndex:indexPath.row]];
cell.images.image = [UIImage imageNamed:[self.imagePaths objectAtIndex:index path.row]];
return cell;
}