0

我尝试使用 .plist 中的部分制作 UICollectionView。一切功能都必须如此。

但我收到警告:

NSString分配给from UILabel_strong的不兼容指针类型。

有人知道为什么吗?

我将不胜感激任何帮助...

问题在这里:

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
4

1 回答 1

1

您正在尝试将 UILabel 实例 (nameLabel1) 分配给另一个标签文本,即 NSString。尝试将 nameLabel1 的声明更改为 NSString 类型。

NSString *nameLabel1 = nil;

而且您可能希望更改变量名称以匹配类型,这样您就不会轻易混淆。例如:

UIImage *firstImage  = nil;
NSString *firstNameString = nil;
于 2013-06-05T00:36:28.910 回答