繁体   English   中英

关闭ImagePicker后添加数据并重新加载CollectionView-Swift-iOS

[英]Add data and reload CollectionView after dismiss ImagePicker - Swift - iOS

在我的应用程序中,用户可以使用图像选择器选择图像。 他选择之后,我想用新图像更新我的collectionview(或tableview)。 但是,即使调用了重装功能,并且新项目在我的列表中,UI也不会刷新。

编辑以突出显示此内容:我将代码简化为cancel dismiss,因为行为相同。 我知道我必须致电didFinishWith ...,但我想强调一下由解雇引起的行为。

var files = [File]() // populate my collectionview or tableview

// ... code
// ... more code 

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {

    self.dismiss(animated: true) {

        let newFile = File(name: "test")
        self.files.append(newFile)

        DispatchQueue.main.async {
            self.collectionView?.reloadData()
            // self.tableView?.reloadData() has the same behavior 
        }
    }
}

如果我在collectionView-CellForItemAt中打印...,我看到列表中有新元素,但是它没有显示在屏幕上。

我尝试不同的东西,但是对我来说什么都不起作用或看起来很肮脏。 有任何想法可以正确执行此操作吗?

(我已经看到了这个主题: UIImagePickerController关闭后,UICollectionView不会reloadData()

编辑更多信息:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return self.files.count
}

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

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemCell", for: indexPath) as! CollectionViewCell

        cell.file = self.files[indexPath.row]
        cell.filenameLabel.text = self.files[indexPath.row].name
        // other customization of cell
        print(cell.file.name)
        return cell
    }

您正在实现imagePickerControllerDidCancel。 如果用户选择图像,则选择器未使用imagePickerControllerDidCancel调用委托,而是调用didFinishPickingMediaWithInfo

试试我的目标C代码:

[objCollectionView reloadData];

[objCollectionView.collectionViewLayout invalidateLayout];

 [objCollectionView performBatchUpdates:^{
                } completion:^(BOOL finished) {

                }];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM