2017-08-11 7 views
0

私はswift 3でコーディングされたプロジェクトで作業しています。ボタンがタップされると画像が変更されるUITableViewCellの中にUIButtonがあります。選択されたボタンは意図したとおりに選択されますが、UITableviewがスクロールすると、セルが再利用されたため選択した画像が消えます。私はプログラミングに慣れていないので、ロジックを書くのに困っています。このコードを参考にしていただければ幸いです。スクロールするとUITableViewCellの選択されたボタンが消えます

CellFoRow

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 

     //Button_Action 
     addSongButtonIdentifier(cell: cell, indexPath.row) 

    } 

セルが作成されている場所です。

func addSongButtonIdentifier(cell: UITableViewCell, _ index: Int) { 
    let addButton = cell.viewWithTag(TABLE_CELL_TAGS.addButton) as! UIButton 

    //accessibilityIdentifier is used to identify a particular element which takes an input parameter of a string 

    //assigning the indexpath button 
    addButton.accessibilityIdentifier = String (index) 
    // print("visible Index:",index) 
    print("Index when scrolling :",addButton.accessibilityIdentifier!) 


      addButton.setImage(UIImage(named: "correct"), for: UIControlState.selected) 


      addButton.setImage(UIImage(named: "add_btn"), for: UIControlState.normal) 


    addButton.isSelected = false 
    addButton.addTarget(self, action: #selector(AddToPlaylistViewController.tapFunction), for:.touchUpInside) 


} 

タップ機能

func tapFunction(sender: UIButton) { 
    print("IndexOfRow :",sender.accessibilityIdentifier!) 
    // if let seporated by a comma defines, if let inside a if let. So if the first fails it wont come to second if let 


    if let rowIndexString = sender.accessibilityIdentifier, let rowIndex = Int(rowIndexString) { 
    self.sateOfNewSongArray[rowIndex] = !self.sateOfNewSongArray[rowIndex]//toggle the state when tapped multiple times 
    } 
    sender.isSelected = !sender.isSelected //image toggle 
    print(" Array Data: ", self.sateOfNewSongArray) 

    selectedSongList.removeAll() 

    for (index, element) in self.sateOfNewSongArray.enumerated() { 
     if element{ 
      print("true:", index) 

      selectedSongList.append(songDetailsArray[index]) 

      print("selectedSongList :",selectedSongList) 
     } 
    } 


} 
+0

once tableview reloadボタン状態の代わりに選択した状態をデータセットに保存する必要があるため、選択が消えます –

+0

選択した項目のインデックスまたは行番号を保存する必要があります。 –

+0

私はその理由を知っていますが、ロジックを書くことに問題があると、コードスニペットは大いに感謝します – danu

答えて

0

あなたは私はあなたがtableView delegateメソッド内の機能addSongButtonIdentifierを呼び出していると信じているので、あなたがtableView delegate内のすべてのこれらのプロパティを設定してはならない問題を引き起こしているこのコードaddButton.isSelected = falseを持っています。

代わりに、最初はストーリーボード自体のように、またはセルクラスにモデルを提供するなどして、各セルに対して1回だけ行う必要があります。

+0

コードスニペットをアップロードすることができます – danu

+1

あなたのコードは正しいですが、あなたが使っている方法は少し間違っています。あなたが私にあなたがどのように関数addSongButtonIdentifierを呼び出しているかを示すなら、アプローチします。 –

+0

私はコードを追加しました – danu

0

コントローラレベルでボタンの選択ステータスを維持する必要があります。テーブルビューを構成するために使用しているモデルを変更する必要があります。

同様のシナリオを作成しました。私は配列のselectionStatusArrayを使用してボタンの選択状態を維持しています。

例:あなたはを設定することができUITableView

class ViewController: UIViewController, UITableViewDataSource 
{ 
    @IBOutlet weak var tableView: UITableView! 
    var selectionStatusArray = [false, false, false, false, false] //Array that maintains the button selection status 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     return selectionStatusArray.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell 
     addSongButtonIdentifier(cell: cell, indexPath.row) 
     return cell 
    } 

    func addSongButtonIdentifier(cell: TableViewCell, _ index: Int) 
    { 
     cell.addButton.tag = index 
     cell.addButton.isSelected = selectionStatusArray[index] 
     cell.tapHandler = { 
      self.selectionStatusArray[$0] = cell.addButton.isSelected 
     } 
    } 
} 

2.カスタムUITableViewCell

class TableViewCell: UITableViewCell 
{ 
    @IBOutlet weak var addButton: UIButton! 
    var tapHandler: ((Int)->())? 

    @IBAction func tapFunction(_ sender: UIButton) 
    { 
     sender.isSelected = !sender.isSelected 
     tapHandler?(sender.tag) 
    } 
} 

を含む

1. UIViewController必要に応じてを入力してください。

関連する問題