5

由于我无法使用任何框架来创建相册,我正在尝试使用 Collection View 创建自己的相册,但我一开始就被卡住了。

我的目标是将我的网络服务中的所有图像显示到我的集合视图中,因为所有图像都显示了,下一步是当有人点击任何单元格时,我可以在新视图中打开它并在所有图像之间导航。

这是我创建的基本代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [collectionController reloadData];
    tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:nil action:@selector(touched)];

    tapGesture.numberOfTapsRequired = 1;


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return 6;
}

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

    static NSString *cellIdentifier = @"Cell";

    CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell.imgCollection setImageWithURL:[NSURL URLWithString:@"http://sallescds.com.br/wp-content/uploads/2012/12/xepop-300x300.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    [cell.imgCollection addGestureRecognizer:tapGesture];

    return cell;
}

-(void)touched:(UIGestureRecognizer *)tap{

    NSLog(@"the touch happened");
}

多谢你们。

4

1 回答 1

14

您的代码中有几件事不正确:

首先initWithTarget:action:不应nil为目标传递值。从文档

目标

一个对象,它是接收者在识别手势时发送的动作消息的接收者。nil 不是有效值

在您的情况下,您应该self作为目标传递,因为您想将消息发送touched:到您的类的当前实例。

其次,您传递给的选择器initWithTarget:action:是错误的。您使用@selector(touched)了,但您的方法实现是- (void)touched:(UIGestureRecognizer *)tap;,选择器是@selector(touched:)(注意:)。

如果您感到困惑,我建议您阅读有关选择器的这个问题。

第三,您不能将单个视图附加UIGestureRecognizer到多个视图(请参阅此 SO 问题)。

因此,为了使其工作,您可以UITapGestureRecognizer为每个集合单元创建一个(可能在子类中)。或者更好的是,实现你的UICollectionViewDelegate方法collectionView:didSelectItemAtIndexPath:

编辑 - 如何实施collectionView:didSelectItemAtIndexPath:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // Bind the collectionView's delegate to your view controller
    // This could also be set without code, in your storyboard
    self.collectionView.delegate = self;
}

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

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}

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

    static NSString *cellIdentifier = @"Cell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    [cell.imgCollection setImageWithURL:[NSURL URLWithString:@"http://sallescds.com.br/wp-content/uploads/2012/12/xepop-300x300.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    return cell;
}

// I implemented didSelectItemAtIndexPath:, but you could use willSelectItemAtIndexPath: depending on what you intend to do. See the docs of these two methods for the differences.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // If you need to use the touched cell, you can retrieve it like so
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];

    NSLog(@"touched cell %@ at indexPath %@", cell, indexPath);
}
于 2013-06-05T08:53:04.640 回答