2012-12-20 3 views
5

UICollectionViewCellのボタンからボタンのクリックイベントを得る方法はありますか?私はペンを使用してコレクションビューを作成しました。セルにはボタンがありますが、そのアクションは呼び出されません。問題は、代議員が呼び出されたことだと思います。これをどうすれば解決できますか?UICollectionViewでボタンをクリック

私が作成した方法:

  1. 追加空のペン先は、コレクションビューセル
  2. を追加しました.hと.Mファイルを作成し、
  3. が書いた
  4. 作成したクラスとして、細胞のペン先のファイルの所有者を作りましたクラス内のアクションアクション

にボタンを接続

  • 私がアクションを得ることができる方法はありますか? 私は何が間違っていますか?あなたがオブジェクトパネルから「コレクションビュー・セル」をドラッグすることにより、ペン先のセルを作成することが重要である

    - (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]; 
    
    } 
    
  • +0

    のですか?処置のリンクを削除して再接続してください。 –

    +0

    質問を編集しました –

    +0

    Close request?どうして? Oh ok編集済み –

    答えて

    10

    は、このようなボタンアクションを追加します。 UIViewを使用し、アイデンティティ・インスペクタでこのセルのクラスを変更するだけで、アクションは機能しません。ここで

    +1

    このコードは必須ではありませんが、Nibだけで可能です。 – MacMark

    20

    +0

    これは私の正確な問題でした。私はこの提案を見つけてうれしいです。 – Hendrix

    1

    はあなたがFileOwnerで機能を持っているSWIFT 3.1のコード

    // 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) 
    } 
    
    関連する問題