2017-08-11 44 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

一旦tableview重新加載您的選擇將會消失,因爲您需要將選定狀態存儲在您的數據集而不是按鈕狀態 –

+0

您需要存儲所選項目的索引或行號。 –

+0

我知道原因,但寫了邏輯的麻煩,代碼片段將不勝感激 – danu

回答

0

你有這樣的代碼addButton.isSelected = false,因爲我相信你調用內部tableView delegate方法的功能addSongButtonIdentifier這是造成問題,你不應該將所有的財產裏面tableView delegate

相反,你應該在開始時進行一次,並且對於每個單元格,例如在故事板本身或向單元類提供模型時,都應該只執行一次。

+0

可以上傳代碼片段 – danu

+1

您的代碼是正確的,但你使用它的方式有點不對,所以我可以建議你一個正確的如果你告訴我你如何調用addSongButtonIdentifier函數。 –

+0

我已添加代碼 – danu

0

您需要在控制器級別保持按鈕選擇狀態。你需要改變你用來配置你的tableview的模型。

我已經創建了一個類似的場景。我使用了一個數組selectionStatusArray來維持按鈕的選擇狀態。

實施例:含有

1. UIViewControllerUITableView

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

可以配置UITableViewCell根據您的要求。