单元格点击中的UICollectionView中的全屏图像

时间:2015-11-06 12:36:00

标签: ios objective-c uicollectionview nsurlsession

我正在使用SDWebImageUICollectionView中显示图片。我得到productImageUrlproductId作为服务器响应。能够在Custom-cell中显示图像,现在我想要的是:

1)在另一个名为UIButton(buyButton)的{​​{1}} UIViewController上以ProductDetailViewController显示大视图中的图片。ProductDetailViewController上显示图片但是我从ProductCollectionViewController传递图像网址的方式不正确我想,请查看代码并建议我采用更好的方法来做到这一点。

2)button click上,将使用我之前获得的productId作为服务器响应对服务器进行调用。(如何通过{{1}到dictId以便我可以调用服务器。)

3)获取ProductDetailViewController,因此可以在多个字典中解析多个值。但如果响应包含多个值,那么only two key-value of an Object as response将是什么。

这是我尝试过的代码。 (对不起,未经优化的代码,仍在学习阶段)

ProductCollectionViewController.m

the optimized way to parse the response

ProductDetailViewController.h

#import "ProductCollectionViewController.h"
#import "ProductCell.h"
#import "UIImageView+WebCache.h"
#import "ProductDetailViewController.h"

@interface ProductCollectionViewController ()

@property(strong, nonatomic) NSMutableArray *productList;

@end
@implementation ProductCollectionViewController

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
static NSString * const reuseIdentifier = @"Cell";

-(void)viewDidLoad
{
[super viewDidLoad];
[self getProductList];
}

-(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)getProductList
{
 NSURL * url = [NSURL URLWithString:@"xxxx.yyyy.zzzz"];

NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];

NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject];

NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
    if (!error)
    {
        NSDictionary *responseJson = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

        NSArray *rsBody = responseJson[@"rsBody"];

        _productList = [NSMutableArray new];

        for(NSDictionary *dict in rsBody)
        {
            NSMutableDictionary *dictUrl=[[NSMutableDictionary alloc]init];
            NSMutableDictionary *dictProductId =[[NSMutableDictionary alloc]init];
            [dictUrl setValue:[dict valueForKey:@"productImageUrl"] forKey:@"url"];
             [dictId setValue:[dict valueForKey:@"productId"] forKey:@"id"];
            [_productList addObject:dictUrl];
            [_productList addObject:dictId];
         }
        NSLog(@"urls for image: %@",_productList );
        [self.collectionView reloadData];
    }}];
[dataTask resume];
}

#pragma mark <UICollectionViewDataSource>

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return _productList.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

ProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

NSURL *imageUrl = [[_productList objectAtIndex:indexPath.row]valueForKey:@"url"];
[cell.productImageView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];

NSString *id =[[_productList objectAtIndex:indexPath.row] valueForKey:@"id"];
cell.productPrice.text= id;

return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showProduct"]) {
    NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems];
    ProductDetailViewController *destViewController = segue.destinationViewController;
    NSIndexPath *indexPath = [indexPaths objectAtIndex:0]
    destViewController.productName =[[_productList objectAtIndex:indexPath.row]valueForKey:@"url"];

    [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}
}
@end

ProductDetailViewController.m

`#import <UIKit/UIKit.h>
@interface ProductDetailViewController : UIViewController
- (IBAction)buyButton:(id)sender;
- (IBAction)closeButton:(id)sender;
@property (weak, nonatomic) IBOutlet UIImageView *productImage;
@property (weak, nonatomic) NSString *productName;
@end`

JSON中的服务器响应格式

- (void)viewDidLoad {
[super viewDidLoad];

self.productImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.productName]]];

 //code to get productId

}
- (IBAction)buyButton:(id)sender {
//code to make server call with productId.
}

1 个答案:

答案 0 :(得分:0)

关于你的第一个问题,这一行

public interface IType { }

public class TypeOne implements IType { }

public class TypeTwo implements IType { }

JavaPairRDD<IType,Long> pairs = originalRows.flatMapToPair(new PairFlatMapFunction<OriginalType,IType,Long>()

正在阻止主线程。这意味着应用程序将在更新屏幕或允许交互之前下载整个图像,这是不好的。

self.productImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.productName]]];

请尝试使用上面的块。它将在不同的线程上获取产品图像。

问题二:视图控制器之间的两个传输数据执行您在prepareForSegue设置目标视图控制器的公共属性时所做的事情。

问题三:最佳方法是创建一个NSObject类,通过一些名为setupFromDictionary的方法,将字典中的数据读入该类的属性。

在这里,您将拥有一个名为product的Object,其中包含productID属性和productImageURL属性。这样你就不会在某些字典上不断调用valueForKey或objectForKey。