2017-02-15 118 views
0

我有一個CollectionView並在此collectionView的每個單元格中有一個textFieldUICollectionView - 如何更新從一個單元格變化的所有單元格

如果textfield的所有內容nil,當我改變(例如cell_1)所有其他textfield的意志爲自動更新內容與textfieldcell_1內容之一textfield內容。

我試過collectionView.visibleCells,但它對我的場景無法正常工作。

有沒有什麼辦法可以改變一個單元,然後更新CollectionView中的所有單元?

請幫忙。提前致謝!

+0

我不是完全跟着你,只要是你的問題,所有的細胞得到** ** cell_1的價值,或者是你希望所有的細胞更新到** cell_1值**爲您在** cell_1 **中輸入「textfield」中的內容? –

+0

@PaulPeelen我想我在cell_1 – javimuu

回答

2

清除新細胞:
因爲我不理解一個100%你的問題,我認爲你的問題是,新的細胞得到cell_1,這是不是你想要的東西的價值。

如果是這種情況,那麼在UICollectionViewCell中有一個稱爲prepareForReuse的函數。 在你的UICollectionViewCell的子類,具有如下功能:

override func prepareForReuse() { 
    super.prepareForReuse() 
    textfield.text = "" 
} 

這應該做的伎倆,因爲你的textfield被稱爲textfield

執行任何必要的清理操作以準備再次使用該視圖。 https://developer.apple.com/reference/uikit/uicollectionreusableview/1620141-prepareforreuse

更新所有單元格:
如果你的問題是如何更新其他細胞獲得相同的內容cell_1,那麼這意味着集合視圖子類需要了解在更新cell_1,然後將該更新轉換爲其他單元格。 這樣做的一種方法是在集合視圖中設置您的textfield的委託。當值更改(textField(_:shouldChangeCharactersIn:replacementString:))時,獲取所有可見單元格並相應地更新那些textfields。也許還可以在cell_1中保存對textfield的引用,或者在集合視圖中保存一個字符串變量,說明最新輸入的值,以便可以在顯示單元格時更新單元格。 你不要雖然想使用reloadData(),因爲這將從textField刪除焦點,因爲textField重新加載。

解決方案:

class ViewController: UIViewController { 
    @IBOutlet weak var collectionView: UICollectionView! 

    fileprivate var customString: String? 
} 

extension ViewController: UICollectionViewDataSource { 

    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return 1 
    } 

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell 

     cell.textField.delegate = self 
     cell.textField.text = customString 

     return cell 
    } 

} 

extension ViewController: UITextFieldDelegate { 

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 

     customString = string 

     for cell in collectionView.visibleCells { 
      if let cell = cell as? CustomCell, 
       let text = textField.text, 
       cell.textField != textField { 
       cell.textField.text = text+string 
      } 
     } 

     return true 
    } 

} 

在上面的代碼唯一con是,它遍歷當前可見細胞。通常我會建議不要循環,除非絕對必要,但上面解決方案的替代方法是發佈通知並讓單元監聽該通知並相應地更新其內容。這樣做不僅會矯枉過正,而且還有一些通知並非真正意義上的。第三種選擇是將數組中的弱引用保存到每個文本字段中,這可能不是最優的,因爲它會產生不必要的開銷。畢竟visibleCells已經是當前可見的單元格陣列,每個單元格都擁有對UITextField的引用。

1

試試這個 -

第1步 -

創建像和您的收藏觀IBOutlet中的變量 -

var xyzString: String? 
@IBOutlet weak var collectionView: UICollectionView! // IBOutlet of your collection view 

第2步 -

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TagViewCell", for: indexPath) as? TagViewCell // instead of TagViewCell give your custom cell class name 
    if self.xyzString != nil { 
     cell?.txtField.text = self.xyzString // if you want same value for all cell 
    } 
    return cell! 
} 

第3步 -

func textFieldDidEndEditing(_ textField: UITextField) { 
    self.xyzString = textField.text 
    self.collectionView.reloadData() 
} 
+0

文本字段將失敗進入出頭所有的細胞更新到cell_1的價值。 'cell?.txtField'將是'UITextField'類型,它不符合'String'。此外,由於「UITextField.text」是可選類型「String」,所以不需要'if case'。設置'cell?.txtField.text = xyzString'應該足夠了,因爲'xyzString'也是可選的'String'。另外,除非焦點從UITextField中刪除,否則'xyzString'不會被更新,如果我沒有誤解的話。 –

+0

@PaulPeelen - 感謝您的反饋。 :) –

+0

您的歡迎=) –

相關問題