2012-12-20 28 views
5

पर क्लिक करें क्या कोई तरीका है कि मैं UICollectionViewCell के अंदर बटन से बटन क्लिक ईवेंट प्राप्त कर सकता हूं? मैंने संग्रह दृश्य को पॉप्युलेट करने के लिए एक नीब का उपयोग किया, सेल में बटन है लेकिन इसकी कार्रवाई को कॉल नहीं किया जा रहा है। मुझे लगता है कि समस्या प्रतिनिधि के साथ है। मैं इसे कैसे ठीक करूं?प्राप्त करें बटन UICollectionView

मैं कैसे बनाई:

  1. जोड़ा एक खाली निब, एक संग्रह को देखने सेल
  2. जोड़ा एक ज और .m फ़ाइल बनाया है और बनाया
  3. लिखा वर्ग के रूप में सेल निब की फाइलें स्वामी बना दिया कक्षा में एक कार्रवाई।
  4. कार्रवाई

वहाँ एक रास्ता मैं कार्रवाई प्राप्त कर सकते हैं है करने के लिए बटन कनेक्ट है? मैं क्या गलत कर रहा हूँ?

+0

है आप FileOwner में समारोह है? कृपया लिंक को क्रिया को हटाने का प्रयास करें और इसे पुनः कनेक्ट करें। –

+0

प्रश्न संपादित किया गया –

+0

बंद अनुरोध? क्यूं कर? ओह ठीक संपादित –

उत्तर

10

इस तरह बटन कार्रवाई करें:

- (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

यह कोड आवश्यक नहीं है कि यह सिर्फ निब के साथ संभव है। – MacMark

20

यह महत्वपूर्ण है कि आप वस्तुओं पैनल से एक "संग्रह देखें सेल" खींचकर नोक में सेल पैदा करते हैं। यदि आप UIView का उपयोग करते हैं और पहचान कक्ष में इस सेल के लिए कक्षा को बदलते हैं तो कार्रवाई काम नहीं करेगी।

+0

यह मेरा सटीक मुद्दा था। खुशी है कि मुझे यह सुझाव मिला। – Hendrix

1

यहाँ तेजी से 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) 
}