2015-07-18 10 views
5

मैं चेतावनीसंग्रह दृश्य से आइटम को कैसे हटाएं?

में उपयोगकर्ता पसंद के आधार पर collectionview से आइटम को हटाने के लिए मैं कोड निम्नलिखित है की कोशिश

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    let person = people[indexPath.item] 

    let questionController = UIAlertController(title: "What u wanna do?", message: nil, preferredStyle: .Alert) 
    questionController.addAction(UIAlertAction(title: "Rename person", style: .Default, handler: { 

     (action:UIAlertAction!) -> Void in 

     let ac = UIAlertController(title: "Rename person", message: nil, preferredStyle: .Alert) 
     ac.addTextFieldWithConfigurationHandler(nil) 

     ac.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)) 
     ac.addAction(UIAlertAction(title: "OK", style: .Default) { [unowned self, ac] _ in 
      let newName = ac.textFields![0] as! UITextField 
      person.name = newName.text 

      self.collectionView.reloadData() }) 

     self.presentViewController(ac, animated: true, completion: nil) 

    })) 

    questionController.addAction(UIAlertAction(title: "Delete Person", style: .Default, handler: { 

     (action:UIAlertAction!) -> Void in 

     println("hello world") 
     self.collectionView.deleteItemsAtIndexPaths([indexPath.item]) 
     self.collectionView.reloadData() 

    })) 

    presentViewController(questionController, animated: true, completion: nil) 
} 

हैलो दुनिया ठीक है और एप्लिकेशन क्रैश काम करता है जब मैं व्यक्ति को हटाना प्रेस

सांत्वना उत्पादन

है
hello world 
2015-07-18 13:40:14.628 Project10[15888:1274436] -[__NSCFNumber section]:   unrecognized selector sent to instance 0xb000000000000003 
2015-07-18 13:40:14.636 Project10[15888:1274436] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber section]: unrecognized selector sent to instance 0xb000000000000003' 

मैं क्या गलत कर रहा हूं?

उत्तर

7

आप बदलना चाहिए

self.collectionView.deleteItemsAtIndexPaths([indexPath.item]) 

को
self.collectionView.deleteItemsAtIndexPaths([indexPath]) 

deleteItemsAtIndexPathsNSIndexPath रों, नहीं संख्या की एक सरणी की एक सरणी की उम्मीद है।

इसके अलावा, यदि आप deleteItemsAtIndexPaths पर कॉल करते हैं तो आपको reloadData पर कॉल की आवश्यकता नहीं है - यह किसी भी एनीमेशन को होने से भी रोक देगा।

अपने डेटा स्रोत को अपडेट करना न भूलें - व्यक्ति को people सरणी से हटा दिया जाना है।

people.removeAtIndex(indexPath.item) 

बुला deleteItemsAtIndexPaths से पहले इस करो।

+1

मदद नहीं की असीमित अपवाद 'एनएसआईएन इंटरनेशनल इन्कॉन्सिस्टेंसी अपवाद' के कारण ऐप को समाप्त करना, कारण: 'अमान्य अद्यतन: खंड 0 में आइटम की अमान्य संख्या अद्यतन (1) के बाद मौजूदा खंड में निहित वस्तुओं की संख्या बराबर होनी चाहिए अद्यतन (1) से पहले उस खंड में निहित वस्तुओं की संख्या, उस अनुभाग से सम्मिलित या हटाए गए आइटमों की संख्या या घटाएं (0 डाला गया, 1 हटाया गया) और उस अनुभाग के अंदर या बाहर स्थानांतरित वस्तुओं की संख्या को घटाएं या घटाएं (0 में स्थानांतरित हो गया, 0 बाहर चले गए)। ' –

+0

अपना डेटा स्रोत अपडेट करना न भूलें - व्यक्ति को 'लोगों' सरणी से हटा दिया जाना है। – Glorfindel

+0

क्षमा करें, मैं तेजी से नया हूं - डेटा स्रोत कैसे अपडेट करें? –

संबंधित मुद्दे