Khoury Khoury - 5 months ago 15
Swift Question

How to create a new cell in UICollectionView when a new string is added to an array

I have a

UICollectionView
with the cell count being equal to the number of strings in a certain array.

When I append a new string to the array, I am expecting it to create a new cell, but it does not.

func addDateToArray(dateString: String) {
Globals.datesArray.append(dateString)
NSUserDefaults.standardUserDefaults().setObject(Globals.datesArray, forKey: "saveddates")
}


I am appending from another view

@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)


I don't know why this doesn't work. If I haven't been specific enough, let me know to provide more info.

Answer

To add a new cell and do reloadData() would work, but it is very inefficient.

You can try something like this after appending. :

let count = yourStringArray.count
let index = count > 0 ? count - 1 : count
let indexPath = NSIndexPath(forItem: index, inSection: 0)
collectionView!.insertItemsAtIndexPaths([indexPath]) // This will call dataSource methods itself

Ask questions, if any doubt!

Edit:

Carrying on with suggestions in the comment, you can add observer in you collectionView class like so in 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])

}

Then in your other class you can do this:

NSNotificationCenter.defaultCenter().postNotificationName("load", object: nil)

Extra: Take a note, that you can send objects through notifications. You can send objects and append to array in the collection view class itself

like so? :

NSNotificationCenter.defaultCenter().postNotificationName("load", object: someDateofNSDateType)

And then you can receive this date and append in the very class(your collectionview class)

Comments