2012-12-20 33 views
5

Có cách nào tôi có thể nhận được sự kiện bấm nút từ một nút bên trong một UICollectionViewCell không? Tôi đã sử dụng một nib để điền vào khung nhìn bộ sưu tập, ô có nút nhưng hành động của nó không được gọi. Tôi nghĩ rằng vấn đề là với các đại biểu được gọi là. Làm thế nào tôi có thể sửa lỗi này?Đạt được nút bấm trong UICollectionView

Làm thế nào tôi tạo ra:

  1. Added một nib trống, tạo ra một tế bào xem bộ sưu tập
  2. Added một .h và file .m và làm chủ sở hữu file nib tế bào như các lớp tạo
  3. Wrote một hành động trong lớp.
  4. Đã kết nối nút với hành động

Có cách nào để tôi có thể thực hiện hành động không? Tôi đang làm gì sai?

+0

Bạn có các chức năng trong FileOwner? Vui lòng thử xóa liên kết để hành động và kết nối lại. –

+0

Đã chỉnh sửa câu hỏi –

+0

Đóng yêu cầu? Tại sao? Oh ok Đã chỉnh sửa –

Trả lời

10

Thêm button action như thế này:

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

    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellId" forIndexPath:[indexPath row]]; 

    [[cell myButton] addTarget:self action:@selector(myClickEvent:event:) forControlEvents:UIControlEventTouchUpInside]; 

    return cell; 

} 


- (IBAction)myClickEvent:(id)sender event:(id)event { 

    NSSet *touches = [event allTouches]; 

    UITouch *touch = [touches anyObject]; 

    CGPoint currentTouchPosition = [touch locationInView:_myCollectionArray]; 

    NSIndexPath *indexPath = [_myCollectionArray indexPathForItemAtPoint: currentTouchPosition]; 

} 
+1

Mã này là không cần thiết, nó có thể chỉ với Nib. – MacMark

20

Điều quan trọng là bạn tạo ra các tế bào trong Nib bằng cách kéo một "Bộ sưu tập Xem di động" từ bảng điều khiển đối tượng. Nếu bạn sử dụng một UIView và chỉ cần thay đổi lớp cho ô này trong Identity Inspector thì hành động sẽ không hoạt động.

+0

Đây là vấn đề chính xác của tôi. Rất vui vì tôi đã tìm thấy đề xuất này. – Hendrix

1

Dưới đây là nhanh chóng 3.1 đang

// make a cell for each cell index path 
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    // get a reference to our storyboard cell 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! BlueCircleViewCell 

    // Use the outlet in our custom class to get a reference to the UILabel in the cell 
    cell.bgImage.image = UIImage(named: items[indexPath.row]) 
    cell.addButton.addTarget(self, action: #selector(addCircle(_:)), for: .touchUpInside) 

//  cell.backgroundColor = UIColor.cyan // make cell more visible in our example project 

    return cell 
} 

func addCircle(_ sender:UIButton){ 
    //CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView]; 
    let buttonPosition:CGPoint = sender.convert(.zero, to: self.collectionView) 
    let indexPath:IndexPath = self.collectionView.indexPathForItem(at: buttonPosition)! 
    onAddBlueCircle(indexPath: indexPath) 
}