Ios 使用AFNetworking在collectionView中追加图像
我是Ios应用程序开发新手。我正在使用AFNetworking进行图像和数据加载,数据在采集视图中被装箱,但无法绑定图像。 这是我的集合视图和服务代码:Ios 使用AFNetworking在collectionView中追加图像,ios,afnetworking,Ios,Afnetworking,我是Ios应用程序开发新手。我正在使用AFNetworking进行图像和数据加载,数据在采集视图中被装箱,但无法绑定图像。 这是我的集合视图和服务代码: #import "ViewController.h" #import "LabelCollectionViewCell.h" @interface ViewController () { NSMutableArray *idArray; NSMutableArray *namelabelArray; UIImage *i
#import "ViewController.h"
#import "LabelCollectionViewCell.h"
@interface ViewController ()
{
NSMutableArray *idArray;
NSMutableArray *namelabelArray;
UIImage *imagArray;
NSMutableArray *dic ;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[self userCollection]setDataSource:self];
[[self userCollection]setDelegate:self];
[self dataJson];
NSLog(@"array: %@", namelabelArray);
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [namelabelArray count];
}
-(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";
LabelCollectionViewCell *customCell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
[[customCell nameLabel]setText:[namelabelArray objectAtIndex:indexPath.item]];
[[customCell idLabel]setText:[idArray objectAtIndex:indexPath.item]];
return customCell;
}
-(void)dataJson
{
NSString *zipcode;
NSString *url = [NSString stringWithFormat:@"%@",@"http://inveera.biz/lowkall_api/index.php/product_cat"];
NSDictionary *params =[NSDictionary dictionaryWithObjectsAndKeys: zipcode,@"35005",nil];
NSLog(@"Login URL %@", params);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Login JSON: %@", responseObject);
NSString *data = [responseObject valueForKey:@"data"];
idArray = [data valueForKey:@"id"];
namelabelArray =[data valueForKey:@"name"];
imagArray =[data valueForKey:@"img_path"];
NSLog(@"Error image: %@", imagArray);
[self.userCollection reloadData];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error: %@", error);
}];
}
我的json响应是
{
"status":"true",
"city":[{"cityname":"Acmar"}],
"data":[
{"id":"1","name":"Appliances","img_path":"categories\/1\/ac1.png","status":"1"},
{"id":"2","name":"Electronics","img_path":"categories\/2\/ac2.png","status":"1"},
{"id":"3","name":"Furniture","img_path":"categories\/3\/ac3.png","status":"1"},
{"id":"4","name":"Cars","img_path":"categories\/4\/ac4.png","status":"1"},
{"id":"5","name":"Pet Supplies","img_path":"categories\/5\/ac5.png","status":"1"},
{"id":"6","name":"Others","img_path":"categories\/6\/ac6.png","status":"1"}
]
}
我可以绑定名称,但无法将图像与域的完整路径绑定
名称
请帮帮我。提前感谢。首先,不要使用
UIImage*imageArray
应该是NSString*imageURLString代码>
然后,由于您使用的是AFNetworking,因此可以更好地异步加载图像(使用#import UIImageView+AFNetworking.h
)
首先,代替UIImage*imageArray
应该是NSString*imageURLString代码>
然后,由于您使用的是AFNetworking,因此可以更好地异步加载图像(使用#import UIImageView+AFNetworking.h
)
在AFNetworking库中,他们为UIImageView添加了一个名为UIImageView+AFNetworking的类别类,以从URL加载图像,您需要使用有效的URL请求调用下面的方法
- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest
placeholderImage:(UIImage *)placeholderImage
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure
-(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";
LabelCollectionViewCell *customCell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
[[customCell nameLabel]setText:[namelabelArray objectAtIndex:indexPath.item]];
[[customCell idLabel]setText:[idArray objectAtIndex:indexPath.item]];
[[customCell imageView] setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[imagArray objectAtIndex:indexPath.row]]]
placeholderImage:nil
success:nil
failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
}];
return customCell;
}
在AFNetworking库中,他们为UIImageView添加了一个名为UIImageView+AFNetworking的类别类,以从URL加载图像,您需要使用有效的URL请求调用下面的方法
- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest
placeholderImage:(UIImage *)placeholderImage
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure
-(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";
LabelCollectionViewCell *customCell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
[[customCell nameLabel]setText:[namelabelArray objectAtIndex:indexPath.item]];
[[customCell idLabel]setText:[idArray objectAtIndex:indexPath.item]];
[[customCell imageView] setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[imagArray objectAtIndex:indexPath.row]]]
placeholderImage:nil
success:nil
failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
}];
return customCell;
}
对不起,Narayana,但我不能这么做。你能给我一些类似collectionView中json绑定的演示吗?或者给我一些代码。我已经正确地使用了这些代码。我没有得到我正在集合视图单元格中加载图像,但是参数获取错误。你能给我发来集合视图中正确的数据加载吗(实际上,所有参数都在不同的数组中传递,我只想使用合适的数组和带有参数的字典加载)Plazease Shere mecity=({cityname=”“;});2015-12-16 12:25:11.353 LowKall应用程序[2563:49732]-[\uu NSCFString objectForKey:]:发送到实例0x7f997ad63a90 2015-12-16 12:25:11.391 LowKall应用程序[2563:49732]***由于未捕获异常“NSInvalidArgumentException”终止应用程序,原因:'-[\u NSCFString objectForKey:]:无法识别的选择器已发送到实例0x7f997ad63a90'***第一次抛出调用堆栈:@Udaikumar您可以将您的cellForItemAtIndexPath代码发送到此处吗抱歉Narayana,但我无法执行此操作。您可以在collectionView中为json绑定提供任何演示或提供代码吗?请我已正确使用此代码我不明白我正在加载Imag收集视图单元格上的e,但参数获取错误您能否在收集视图上向我发送正确的Afnetworking数据加载(实际上,所有参数都传递到不同的数组中,我只想使用正确的数组和带有参数的字典加载)Plazease Shere mecity=({cityname=”“;});2015-12-16 12:25:11.353 LowKall应用程序[2563:49732]-[\uu-NSCFString objectForKey:]:发送到实例0x7f997ad63a90的未识别选择器2015-12-16 12:25:11.391 LowKall应用程序[2563:49732]***由于未捕获的异常“NSInvalidArgumentException”终止应用程序,原因:'-[\uu-NSCFString objectForKey:]:无法识别的选择器已发送到实例0x7f997ad63a90'***第一次抛出调用堆栈:@Udaikumar您能在此处发送cellForItemAtIndexPath代码吗