swift – 如何从集合视图中删除项目?

swift – 如何从集合视图中删除项目?,第1张

概述我尝试根据用户在alert中的选择从collectionview中删除项目 我有以下代码 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { let person = people[indexPath.item] let q 我尝试根据用户在alert中的选择从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",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",handler: {        (action:UIAlertAction!) -> VoID in        println("hello world")        self.collectionVIEw.deleteItemsAtIndexPaths([indexPath.item])        self.collectionVIEw.reloadData()    }))    presentVIEwController(questionController,completion: nil)}

当我按删除人员时,你好世界工作正常和应用程序崩溃

控制台输出是

hello world2015-07-18 13:40:14.628 Project10[15888:1274436] -[__NSCFNumber section]:         unrecognized selector sent to instance 0xb0000000000000032015-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'

我究竟做错了什么 ?

你应该改变
self.collectionVIEw.deleteItemsAtIndexPaths([indexPath.item])

self.collectionVIEw.deleteItemsAtIndexPaths([indexPath])

deleteItemsAtIndexPaths需要一个NSIndexPaths数组,而不是数组.

除此之外,如果你调用deleteItemsAtIndexPaths,你不需要调用reloadData – 这甚至可以防止任何动画发生.

不要忘记更新数据源 – 必须从人员阵列中删除此人.

people.removeAtIndex(indexPath.item)

在调用deleteItemsAtIndexPaths之前执行此 *** 作.

总结

以上是内存溢出为你收集整理的swift – 如何从集合视图中删除项目?全部内容,希望文章能够帮你解决swift – 如何从集合视图中删除项目?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1032524.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存