我有一个包含图像值路径的数组,我想在collection view
单元格中动态显示这些图像。但我遇到了一个例外。
我的代码是:
- (void)viewDidLoad
{
[super viewDidLoad];
data=[MyDatabase new];
imagearray=[data OpenMyDatabase:@"SELECT pic_url FROM interior":@"pic_url"];
gallerycollection.dataSource=self;
[[self gallerycollection]setDelegate:self];
gallerycollection.backgroundColor=[UIColor clearColor];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=@"Cell";
customcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
// [[cell nyimage]setImage:[UIImage imageNamed:[imagearray objectAtIndex:indexPath.row]]];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:1];
recipeImageView.image=[imagearray objectAtIndex:indexPath.section * 1 + indexPath.row];
return cell;
}
我得到了例外
2013-03-13 11:22:30.662 Home[3371:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[customcell setImage:]: unrecognized selector sent to instance 0x9f7e410
@Mithun MP:这些是我的数据库文件
MyDatabase.h
#import "FMDatabase.h"
@interface MyDatabase : NSObject
@property (strong,nonatomic) NSString *databaseName,*databasePath;
-(NSMutableArray *) OpenMyDatabase: (NSString *) query: (NSString *) column;
-(FMDatabase *)openDatabase: (NSString *)databasename;
-(NSMutableArray *) storeDatabaseColumnInArray: (FMDatabase *) database: (NSString *) sqlStatement: (NSString *) columnName;
@end
MyDatabase.m
#import "MyDatabase.h"
#import "FMDatabase.h"
@implementation MyDatabase
-(NSMutableArray *)OpenMyDatabase: (NSString *)query: (NSString *)column
{
//store the database name here
self.databaseName=@"App_Database.sqlite";
//get the reference of the database to be stored in device or emulator
FMDatabase *database=[self openDatabase: self.databaseName];
//open the database
[database open];
//fetch a collumn from database and store it in array
// NSArray *nsarr= [self storeDatabaseColumnInArray: database: @"select id from kas":@"id"];
NSArray *nsarr= [self storeDatabaseColumnInArray: database: query: column];
//close the database
[database close];
return nsarr;
}
- (FMDatabase *)openDatabase: (NSString *)databasename
{
NSFileManager *fm = [NSFileManager defaultManager];
NSString *documents_dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *db_path = [documents_dir stringByAppendingPathComponent: databasename ];
NSString *template_path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: databasename];
if (![fm fileExistsAtPath:db_path])
[fm copyItemAtPath:template_path toPath:db_path error:nil];
FMDatabase *db = [FMDatabase databaseWithPath:db_path];
if (![db open])
NSLog(@"Failed to open database!");
return db;
}
-(void) createAndCheckDatabase
{
BOOL success;
NSFileManager *fileManager=[NSFileManager defaultManager];
success=[fileManager fileExistsAtPath:_databasePath];
NSString *databasePathFromApp=[[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:self.databaseName ];
[fileManager copyItemAtPath:databasePathFromApp toPath:_databasePath error:nil];
}
-(NSMutableArray *) storeDatabaseColumnInArray : (FMDatabase *) database: (NSString *) sqlStatement: (NSString *) columnName
{
NSMutableArray *mutablearray = [NSMutableArray array];
FMResultSet *results = [database executeQuery: sqlStatement];
while([results next]) {
NSString *rowValue = [results stringForColumn:columnName];
[mutablearray addObject:results];
NSLog(@"row value %@",rowValue);
}
//convert the nsmutable array to nsarray
NSMutableArray *temparr=[NSMutableArray arrayWithArray:mutablearray];
return temparr;
}
@end