Ios 向数组中添加新字符串时如何在UICollectionView中创建新单元格
我有一个Ios 向数组中添加新字符串时如何在UICollectionView中创建新单元格,ios,arrays,swift,uicollectionview,uicollectionviewcell,Ios,Arrays,Swift,Uicollectionview,Uicollectionviewcell,我有一个UICollectionView,单元格计数等于某个数组中的字符串数 当我将一个新字符串附加到数组中时,我希望它创建一个新的单元格,但它没有 func addDateToArray(dateString: String) { Globals.datesArray.append(dateString) NSUserDefaults.standardUserDefaults().setObject(Globals.datesArray, forKey: "saveddates
UICollectionView
,单元格计数等于某个数组中的字符串数
当我将一个新字符串附加到数组中时,我希望它创建一个新的单元格,但它没有
func addDateToArray(dateString: String) {
Globals.datesArray.append(dateString)
NSUserDefaults.standardUserDefaults().setObject(Globals.datesArray, forKey: "saveddates")
}
我是从另一个角度附加的
@IBAction func SetDate(sender: AnyObject) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let day = dateTimePicker.date
let datestring = dateFormatter.stringFromDate(day)
addDateToArray(datestring)
我不知道为什么这样不行。如果我不够具体,请告诉我提供更多信息。要添加新单元格,您可以重新加载数据()
,这样就可以了!——但是它的效率非常低(您只想更新一个项目,而不是整个collectionView(想象一下,如果在collection view中加载1000个项目,只添加第1001个项目),因此一定有更好的方法
这就是为什么我们有insert函数,要使用它->您可以在添加后尝试类似这样的操作
let count = yourStringArray.count // updated Count of array
let index = count > 0 ? count - 1 : count // find the index, where you want to add the item.
let indexPath = NSIndexPath(forItem: index, inSection: 0) // make an indexPath out of the found index!
collectionView!.insertItemsAtIndexPaths([indexPath]) // This will call dataSource methods itself
// And you should be good to go!
如有疑问,提出问题:-)
编辑:从另一个类更新集合视图的代码
继续评论中的建议,您可以在collectionView类中添加observer,如viewDidLoad
:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourCollectionViewController.loadList(_:)),name:"load", object: nil)
// And add this function in class
func loadList(notification: NSNotification){
let count = yourStringArray.count
let index = count > 0 ? count - 1 : count
let indexPath = NSIndexPath(forItem: index, inSection: 0)
collectionView!.insertItemsAtIndexPaths([indexPath])
}
然后在你的其他课程中,你可以这样做:
NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
额外:
请注意,您可以通过通知发送对象。您可以发送对象并附加到集合视图类本身中的数组
是这样吗
NSNotificationCenter.defaultCenter().postNotificationName("load", object: someDateasStringType)
然后,您可以接收此日期并将其附加到该类(您的collectionview类)中,如下所示:
func loadList(notification: NSNotification){
let returnedDate = notification.object as! String
// convert string to Date. and append and insert to collection View (all mentioned above.)
}
[不推荐]
重新加载数据
将在集合/表格视图中重新加载所有数据
可以使用在两个不同视图之间进行调用。在UICollectionView
方法中添加以下代码:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:", name:"load", object: nil)
然后将此方法进一步添加到UIViewContoller
:
func loadList(notification: NSNotification){
self.scollview.reloadData()
}
当您要重新加载集合视图时,从其他类:
NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)
在数组
collview.reloadData()
中添加新字符串时,必须在数据更改后重新加载集合视图:collectionViewVariable.reloadData()@BhavinRamani,这是一个快速的问题。请快速回答我如何从另一个类重新加载集合视图?使用NSNotificationCenter刷新tableView或使用委托,您可以从另一个类刷新tableView(),如果我从另一个视图追加,我将把它放在哪里?显示您的代码,您如何从另一个类追加?