【问题标题】:How to create a new cell in UICollectionView when a new string is added to an array将新字符串添加到数组时如何在 UICollectionView 中创建新单元格
【发布时间】:2016-06-28 07:44:06
【问题描述】:

我有一个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)

我不知道为什么这不起作用。如果我不够具体,请告诉我以提供更多信息。

【问题讨论】:

  • 在数组collview.reloadData()追加新字符串时添加这个
  • 数据更改后必须重新加载集合视图:collectionViewVariable.reloadData()
  • @BhavinRamani,这是一个快速的问题。请尽快回复
  • 如何从另一个类重新加载集合视图?
  • 使用 NSNotificationCenter 刷新 tableView 或者使用委托你可以从另一个类刷新 tableView (stackoverflow.com/questions/24049020/…)

标签: ios arrays swift uicollectionview uicollectionviewcell


【解决方案1】:

要添加新单元格,您可以 reloadData(),它工作! --- 但它 非常低效 :( 您只想更新 一个 项而不是整个 collectionView(想象一下,如果集合中有 1000 个项视图加载只是添加第 1001 个项目)。所以一定有更好的东西

这就是为什么,我们有插入功能,并且要使用它 -> 你可以尝试像这样 after appending。 :

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 类中添加观察者,就像在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.)
}

【讨论】:

  • 如果我从另一个视图追加,我会把它放在哪里?
  • 显示你的代码,你是如何从另一个类追加的?
【解决方案2】:

[不推荐] - reloadData 将重新加载您在 Collection / Table View 中的所有数据

您可以使用NSNotificationCenter 在两个不同的视图之间进行调用。在你有UICollectionViewviewDidLoad方法中添加以下代码:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadList:", name:"load", object: nil)

然后将此方法进一步添加到您的UIViewContoller

func loadList(notification: NSNotification){
    self.scollview.reloadData()
}

当你想重新加载集合视图时,来自其他类:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 2022-01-08
    • 2022-01-16
    相关资源
    最近更新 更多