2016-10-28 24 views
0

在我的應用程序中,用戶可以用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 
    } 
+0

你應該使用這種方法:'didFinishPickingMediaWithInfo'並顯示你的代碼'CellForItemAt' –

+0

@Lumem我認爲這種類型的要求,你應該去與PHASSET –

+0

正如我所說,我只是告訴你的例子與取消,以簡化理解。 – Lyume

回答

1

要實現imagePickerControllerDidCancel。如果用戶選取的圖像拾取器不會調用與imagePickerControllerDidCancel代表,它調用didFinishPickingMediaWithInfo

+0

我知道,我只是簡化了代碼,因爲行爲是一樣的。 – Lyume

+0

而不是.reloadData()調用collectionView.insertItems(at :)如果您的模型與collectionView不匹配(此情況可能如此),此方法將會斷言。斷言應該告訴你,如果你缺乏細胞。 –

0

試試我的目標C代碼:

[objCollectionView reloadData]; 

[objCollectionView.collectionViewLayout invalidateLayout]; 

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

       }]; 
+0

謝謝,但沒有任何變化。我仍然有我的對象在控制檯中,但不是在UI中。 – Lyume

+0

self.files.append(newFile)這有什麼用?什麼是self.files類型? – KKRocks

+0

self.files填充我的collectionView,並且是[File]的類型。文件是一個簡單的對象與名稱,圖像等 我編輯我的文章,所以你可以看到我在哪裏使用self.files – Lyume

相關問題