【发布时间】:2016-10-28 07:27:40
【问题描述】:
在我的应用中,用户可以使用 imagepicker 选择图像。在他选择之后,我想用新图像更新我的收藏视图(或表格视图)。 但即使调用了重新加载函数并且新项目在我的列表中,UI 也不会刷新。
编辑突出显示:我使用取消关闭来简化代码,因为行为是相同的。 我知道我必须调用 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 中打印...我看到列表中有我的新元素,但它没有显示在屏幕上。
我尝试了不同的东西,但没有任何效果或对我来说似乎很脏。有什么想法可以正确执行吗?
(我已经看到这个话题了:UICollectionView won't reloadData() after UIImagePickerController dismisses)
编辑以获取更多信息:
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
}
【问题讨论】:
-
你应该使用这个方法:
didFinishPickingMediaWithInfo并显示你的代码CellForItemAt -
@Lyume 我认为对于这种类型的要求,您应该使用 PHASSET
-
正如我所说,我只是给你看带有取消的例子,以简化理解。
标签: ios swift uitableview uicollectionview reloaddata