2013-06-05 30 views
5

vì tôi không thể sử dụng bất kỳ khung công tác nào để tạo album ảnh, tôi đang cố gắng tạo khung hình của riêng mình bằng Chế độ xem bộ sưu tập, nhưng tôi đã bị kẹt ngay từ đầu.Xử lý Nhấn Cử chỉ trong UICollectionView

Mục tiêu của tôi là hiển thị tất cả hình ảnh từ dịch vụ web vào chế độ xem bộ sưu tập, vì tất cả được hiển thị, bước tiếp theo là khi ai đó chạm vào bất kỳ ô nào, tôi có thể mở nó ở chế độ xem mới và cũng điều hướng giữa tất cả.

đây là mã cơ bản mà tôi đã tạo:

- (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"); 
} 

nhờ chàng trai.

Trả lời

14

Một vài điều là không đúng trong mã của bạn:

Đầu tiên, initWithTarget:action: không nên được thông qua một giá trị nil cho mục tiêu. Từ the docs:

mục tiêu

Một đối tượng đó là người nhận thông điệp hành động gửi bởi người nhận khi nó nhận ra một cử chỉ. nil không phải là giá trị hợp lệ.

Trong trường hợp của bạn, bạn nên chuyển self làm mục tiêu vì bạn muốn gửi thư touched: đến phiên bản hiện tại của lớp học.

Thứ hai, bộ chọn bạn đã chuyển đến initWithTarget:action: là sai. Bạn đã sử dụng @selector(touched) nhưng triển khai phương pháp của bạn là - (void)touched:(UIGestureRecognizer *)tap;, bộ chọn nào là @selector(touched:) (hãy nhớ :).

Tôi khuyên bạn nên đọc this question on selectors nếu bạn đang bối rối.

Thứ ba, bạn không thể đính kèm một đơn UIGestureRecognizer cho nhiều chế độ xem (xem this SO question).

Vì vậy, để làm cho nó hoạt động, bạn có thể tạo một UITapGestureRecognizer cho mỗi ô bộ sưu tập (có thể trong một phân lớp). Hoặc tốt hơn, hãy thực hiện phương pháp UICollectionViewDelegatecollectionView:didSelectItemAtIndexPath: của bạn.

EDIT - Làm thế nào để thực hiện 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); 
} 
+0

Got nó, tôi đã cố định những vấn đề này, sai lầm của tôi khi tôi đã dịch tên các phương pháp của tôi để tiếng anh. Cảm ơn sự giúp đỡ, bây giờ tôi phải tìm ra, làm thế nào để làm điều này trong UICollectionViewDelegate, bởi vì tôi không có ý tưởng. Bạn có thể cho tôi một ví dụ hoặc một cái gì đó như thế? –

+0

Ok, vì vậy tôi phải gọi phương thức này - (BOOL) collectionView: (UICollectionView *) collectionView shouldSelectItemAtIndexPath: (NSIndexPath *) indexPath; và tạo ra cử chỉ bên trong nó? –

+2

Điều đó thực sự đơn giản hơn nhiều. Nếu bạn thực hiện các phương thức đại biểu, bạn không cần trình nhận dạng cử chỉ. Các đại biểu sẽ gọi cho bạn thực hiện các phương pháp của nó khi một liên lạc được phát hiện. Lưu ý rằng bạn không cần phải gọi 'shouldSelect..' hoặc' didSelect..' một cách rõ ràng, đại biểu thực hiện điều này cho bạn. –

Các vấn đề liên quan