2017-08-11 19 views
-2

UITableViewCell内にUIButtonがあり、ボタンをタップすると画像が変更されます。選択したボタンは意図したとおりに選択されますが、UITableViewがスクロールすると、セルが再利用されるため選択した画像が消えます。スクロール時にUITableViewCell画像が消える

ロジックの作成に問題があります。助けてください。

私のコードは、スウィフトに、以下の通りです。3.

CellForRow:

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

複数回同じ質問をしないでください。 https://stackoverflow.com/questions/45627323/selected-button-of-a-uitableviewcell-get-disappear-when-scrolling/45629235#45629235 – PGDev

+1

[スクロールすると[UITableViewCellの選択されたボタンが消える](https ://stackoverflow.com/questions/45627323/selected-button-of-a-uitableviewcell-get-disappear-when-scrolling) – PGDev

答えて

1

、それはaddButton "

1

あなたはarrを持っている必要がありますあなたが持っているselectedSongList配列のようにどのインデックスが選択されているかを格納します。そして、あなたがボタンに選択または選択解除状態を与えるために、この配列からブール値propartyを使用する必要があなたのcellForRow方法やあなたのaddSongButtonIdentifier方法選択状態に

addButton.isSelected = selectedSongList.contains(indexPath.row) 
0

ベストアプローチは、モデルクラスを使用して維持されますする必要がありますセル内の個々の要素のトラック。しかし、私はあなたに迅速な修正を与えることができます。

このような場所にButtonのカスタムクラスを作成します。

class classname: UIButton { var imageName: String? }

ゴーストーリーボードで今度は、あなたのtapbutton実装に

FUNCのtapFunction(送信者を移動させ、あなたのtableViewCellForIndexPathで

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

    if let imgName = addButton.imageName { 
     addButton.setImage(UIImage(named: imgName), for: UIControlState.normal) 
    } else { 
     addButton.setImage(UIImage(named: "add_btn"), for:UIControlState.normal) 
    } 
    addButton.addTarget(self, action: #selector(AddToPlaylistViewController.tapFunction), for:.touchUpInside) 
return cell 
} 

をクラス名するUIButtonからクラスを変更します。クラス名){

論理エラーが addButton.isSelected = false

がcellForRowAtIndexpath方法は、テーブルがスクロールされるたびに呼び出された、ので、それはaddButton.isSelected = self.sateOfNewSongArray[index]

あるべきラインでfunc addSongButtonIdentifier(cell: UITableViewCell, _ index: Int)機能であり

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.imageName = sender.imageName == "correct" ? "add_btn" : "correct" //image toggle 
sender.setImage(UIImage(named: sender.imageName), for:UIControlState.normal) 

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) 
    } 
} 


} 
1
の選択状態をリセットしています

UITableViewを塗りつぶすためのModelクラスを作成し、そのモデルでUIImageVaraivalsを取得します。このモデルは、現在の画像を保持します。ボタンをクリックするだけで、UIImage変数を現在の画像で変更できます。

関連する問題