2017-01-24 4 views
0

こんにちは、私はスイフトでは新しく、ネットでは長い時間を探しています。しかし、使用しないでください。これを達成する方法を教えてください。私はすでに1つのテキストフィールドを選択したときに、私は何もしませんでしたSwiftコレクションビューを変更するUItextField backgroundColor選択時

この最初のpicが

enter image description here

第二の画像(プリ画像)です

enter image description here

私はそれがすべてのBTNを選択して、1つのテキストフィールド

enter image description here

の選択を解除し、選択したときにすべてのBTN

enter image description here

選択された4つのピックであることを選択したとき、第三PICはあります

今私はPic 2とPic Thirdのようにしかできませんが、私はPicを2つのテキストにすることはできません選択すると、フィールドのテキストが表示されます。

どうやってPic Fourのようにすることができますか?

私はこのようにするためにコレクションビューを使用しています Iveはこれに複数のバリエーションを試しましたが、どれもうまくいかないようです。何か案は?ここ

私のコードは

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MyCollectionViewCell 
     if isselectAll == true{ 
      cell.titleText2.textColor = .white 
      cell.titleText2.backgroundColor = UIColor(red: 153/255, green: 31/255, blue: 37/255, alpha: 1) 
      selectedCell.append(indexPath) 
     } 
     return cell 
    } 
func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) 
    { 
    let cell = collectionView.cellForItem(at: indexPath)! 
     selectedCell.append(indexPath) 
     cell.contentView.backgroundColor = UIColor(red: 153/255, green: 31/255, blue: 37/255, alpha: 1) 
    } 
func collectionView(_ collectionView: UICollectionView,didDeselectItemAt indexPath: IndexPath) 
     { 
     let cell = collectionView.cellForItem(at: indexPath)! 
     if selectedCell.contains(indexPath) { 
      selectedCell.remove(at: selectedCell.index(of: indexPath)!) 
      cell.contentView.backgroundColor = .clear 
     } 

で、それはそれを行うには、何がそれを行うための最善の方法だろう良い習慣だかどうかを知りたいですか?おかげでチームメイト

答えて

0

もセット(このコードを使用テキストフィールドからのデリゲート)

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UITextFieldDelegate { 

@IBOutlet weak var collectedView: UICollectionView! 
var flag = false 

override func viewDidLoad() { 
super.viewDidLoad() 
self.collectedView.delegate = self 
self.collectedView.dataSource = self 
} 

//press selected All Button 
@IBAction func selcetedAll(_ sender: Any) { 
self.flag = true 
self.collectedView.reloadData() 
} 

//MARK: UIcolletionViewDelegate 
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
return 10 //return cell according to your requirement 
} 


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MyCollectionViewCell 
if flag { 
    cell.textF.backgroundColor = UIColor.red 
} 
cell.textF.text = String(indexPath.row) 
return cell 
} 

func textFieldDidBeginEditing(_ textField: UITextField) { 
if textField.backgroundColor == UIColor.red { 
    textField.backgroundColor = UIColor.white 
} else { 
    textField.backgroundColor = UIColor.red 
} 
} 
} 
0

まず、選択して選択解除するときにindexPathを出力します。項目が追加され、期待どおりに削除されているかどうかを確認します。そうでない場合、そのステップに何か問題があります。

配列の値が期待どおりに変更されている場合は、値がわかっていても表示が更新されないことを意味します。

ここで、ブレークポイントを使用して、予想されるコードが実行されているかどうかを確認します。そうであれば、あなたが書いたコード行が間違っていることを意味します。そうでなければ、それはあなたが正しい場所にそれを書かなかったことを意味します。

私はあなたのコードを実行するだけで簡単に答えを出すことができます。しかし、それはあなたがデバッグを学ぶのを助けるものではありません。私が上に述べた手順を実行し、何が起こるか教えてください。

ありがとうございました。

関連する問題