我尝试使用.plist中的部分创建一个UICollectionView。 一切都像它必须的功能。
但我收到了警告:
从NSString
_ strong分配给UILabel
的指针类型不兼容。
有人知道为什么吗?
我很感激任何帮助...
问题在于:
UILabel *nameLabel = (UILabel *)[cell viewWithTag:101];
// here is the warning on nameLabel1...
nameLabel.text = nameLabel1;
NSLog(@"%@", nameLabel1);
我的代码:
#import "MainClickViewController.h"
#define animalsSection 0
#define flowersSection 1
#define buildingsSection 2
#define numberOfSections 3
@interface MainClickViewController (){
NSArray *sections;
}
@end
@implementation MainClickViewController
@synthesize animalArray,buildingArray,flowerArray;
- (void)viewDidLoad
{
[super viewDidLoad];
//Load Dictionary with wood name cross refference values for image name
NSString *plistCatPath = [[NSBundle mainBundle] pathForResource:@"stickerei" ofType:@"plist"];
NSDictionary *creatureDictionary = [[NSDictionary alloc] initWithContentsOfFile:plistCatPath];
self.animalArray = creatureDictionary[@"Animals"];
self.flowerArray = creatureDictionary[@"Flowers"];
self.buildingArray = creatureDictionary[@"Buildings"];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return numberOfSections;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:( NSInteger)section{
switch (section)
{
case animalsSection:
//return [self.bugs count];
return [self.animalArray count];
case flowersSection:
//return [self.animals count];
return [self.flowerArray count];
case buildingsSection:
//return [self.animals count];
return [self.buildingArray count];
default:
return 0;
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImage *imageView1 = nil;
UILabel *nameLabel1 = nil;
switch (indexPath.section)
{
case animalsSection:
nameLabel1 = [animalArray objectAtIndex:indexPath.row][@"StickName"];
imageView1 = [UIImage imageNamed:[animalArray objectAtIndex:indexPath.row][@"StickImage"]];
break;
case flowersSection:
nameLabel1 = [flowerArray objectAtIndex:indexPath.row][@"StickName"];
imageView1 = [UIImage imageNamed:[flowerArray objectAtIndex:indexPath.row][@"StickImage"]];
break;
case buildingsSection:
nameLabel1 = [buildingArray objectAtIndex:indexPath.row][@"StickName"];
imageView1 = [UIImage imageNamed:[buildingArray objectAtIndex:indexPath.row][@"StickImage"]];
break;
}
UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
imageView.image = imageView1;
UILabel *nameLabel = (UILabel *)[cell viewWithTag:101];
// here is the warning on nameLabel1...
nameLabel.text = nameLabel1;
NSLog(@"%@", nameLabel1);
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
答案 0 :(得分:1)
您正在尝试将UILabel实例(nameLabel1)分配给另一个标签文本NSString。尝试将nameLabel1的声明更改为NSString类型。
NSString *nameLabel1 = nil;
而且您可能还想更改变量名称以匹配类型,这样您就不会轻易混淆。例如:
UIImage *firstImage = nil;
NSString *firstNameString = nil;