为什么我的CollectionView没有返回图像?

时间:2016-12-29 20:03:58

标签: ios uicollectionview swift3

我一直在使用Collection View控制器和2个Table View控制器。我想让我的第一个Table View控制器在点击Save按钮(展开segue)时将图像返回到我的Collection View。第二个表视图控制器保存两个图标图像。

现在,在运行时,我可以看到添加的不同单元格(我将灰色背景设置为单元格),但是在第二个TableView控制器中选择的图像不会显示,而是以灰色方块结束。

主集合视图控制器中的代码:

var iconImg = [Icon]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false


    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: UICollectionViewDataSource

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return iconImg.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cellIdentifier = "glanceCell"
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! MasterCollectionViewCell

    // Configure the cell

    return cell
}

// MARK: - Navigation

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "ShowIconDetail" {
        let pickedIconTableViewController = segue.destination as! PickedIconTableViewController

        // Get the cell that generated this segue

        if let selectedCell = sender as? MasterCollectionViewCell {
            let indexPath = collectionView?.indexPath(for: selectedCell)!
            let selectedIconImage = iconImg[((indexPath)?.item)!]
            pickedIconTableViewController.iconImage = selectedIconImage

        }

    }

    else if segue.identifier == "AddIcon" {
        print("Adding new icon")

    }

}

@IBAction func saveToCollectionViewController(_ sender: UIStoryboardSegue) {

    if let sourceViewController = sender.source as? PickedIconTableViewController, let iconImage = sourceViewController.iconImage {

        // Add a new icon image.
        let newIndexPath = IndexPath(item: iconImg.count, section: 0)
        iconImg.append(iconImage)
        collectionView?.insertItems(at: [newIndexPath])

    }

}

@IBAction func cancelToCollectionViewController(_ segue:UIStoryboardSegue) {

}

如果有人可以告诉我显示所选图像的修复方法,请执行!

1 个答案:

答案 0 :(得分:0)

我非常专注于展开segue,我的CellForItemAtIndexPath中的单元格未配置...我缺少以下内容:cell.iconImg.image = iconImg [indexPath.row] .icon。谢谢,如果你困扰阅读:)。