我想在網格視圖中顯示我的圖像。我已經使用UICollection視圖控制器,這是我傳遞的JSON格式的數據,我不能夠使用JSON格式顯示圖像。任何人都可以找到我的錯誤?這裏是我的代碼將Json值傳遞給UICollection查看
@interface ReceipeCollectionViewController()
{
NSMutableData *webdata;
NSURLConnection *connection;
NSMutableArray *contentarray;
NSMutableArray *array;
}
@end
@implementation ReceipeCollectionViewController
(void)viewDidLoad
{
[super viewDidLoad];
[array removeAllObjects];
[[self collectionView]setDelegate:self];
[[self collectionView]setDataSource:self];
array = [[NSMutableArray alloc] init];
}
(void)viewDidAppear:(BOOL)animated
{
NSString *[email protected]"http://wesotech.com/web/myrecipe/web_services/recipe_listing.php?";
NSURL *url=[NSURL URLWithString:serverurl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
connection = [NSURLConnection connectionWithRequest:request delegate:self];
if(connection)
{
webdata=[[NSMutableData alloc]init];
}
else
{
NSLog(@"Connection failure");
}
}
(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webdata appendData:data];
}
(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Fail with error");
}
(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error;
NSArray *results=[NSJSONSerialization JSONObjectWithData:webdata options:0 error:&error];
[array removeAllObjects];
[array addObjectsFromArray:results];
[[self collectionView]reloadData];
}
(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webdata setLength:0];
}
(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [array count];
}
(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
[self.mySpinner stopAnimating];
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell.contentView viewWithTag:100];
recipeImageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[array objectAtIndex:indexPath.row] valueForKey:@"image"]]]];
UITextField *recipeTitleView =(UITextField *)[cell.contentView viewWithTag:101];
recipeTitleView.text=[[array objectAtIndex:indexPath.row]valueForKey:@"recname"];
return cell;
}
@end
我的JSON格式是,
[
{"image":"http:\/\/wesotech.com\/web\/myrecipe\/recipeimages\/2016080614704658059030.jpg","recname":"Italian Chicken","recid":"1"},
{"image":"http:\/\/wesotech.com\/web\/myrecipe\/recipeimages\/2016080614704653421637.jpg","recname":"Fast Food Friday","recid":"2"},
{"image":"http:\/\/wesotech.com\/web\/myrecipe\/recipeimages\/2016080614704648912893.jpg","recname":"Honey Clazed Lamb Chops","recid":"3"}
]
您添加的痕跡或斷點,如果你得到了正確的URL來檢查,如果圖像正確加載中...?另外,你的代碼並不能完全顯示'UIImageView'來自何處/如何。你是否也檢查過它的限制?它可能有正確的圖像,但只有0尺寸或類似的東西。使用視圖調試器將提供更多信息。 – jcaron