Cés*_*ins 5 objective-c uitapgesturerecognizer uicollectionview
因为我无法使用任何框架来创建相册,所以我正在尝试使用Collection View创建自己的框架,但是我在开始时就卡住了.
我的目标是将我的Web服务中的所有图像显示到我的集合视图中,因为所有图像显示,下一步是当有人点击任何单元格时,我可以在新视图中打开它并在所有图像之间导航.
这是我创建的基本代码:
- (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");
}
Run Code Online (Sandbox Code Playgroud)
多谢你们.
Gui*_*gis 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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27251 次 |
| 最近记录: |