在我的應用程序中,用戶可以用imagepicker選擇圖像。在他選擇後,我想用新圖像更新我的collectionview(或tableview)。 但即使調用重新加載funcs並且新項目位於我的列表中,UI也不會刷新。添加數據,並在關閉後重新加載CollectionView ImagePicker - Swift - iOS
編輯突出本:我簡化代碼取消關閉,而不是因爲行爲是相同的。 我知道我必須打電話給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' –
@Lumem我認爲這種類型的要求,你應該去與PHASSET –
正如我所說,我只是告訴你的例子與取消,以簡化理解。 – Lyume