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

有邏輯錯誤在func addSongButtonIdentifier(cell: UITableViewCell, _ index: Int)功能在線路addButton.isSelected = false

應該addButton.isSelected = self.sateOfNewSongArray[index]

由於,的cellForRowAtIndexPath方法被稱爲每次表滾動時,它的復位的「Add按鈕」

1

你需要有arr在哪裏你存儲哪些索引被選中,就像你擁有的selectedSongList數組。然後在您的cellForRow方法,你需要使用布爾proparty此數組中給予或取消選中狀態,以你的按鈕或在您的addSongButtonIdentifier方法選擇的狀態必須

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

最好的方法將使用一個模型類,守信細胞中每個單個元素的軌跡。但讓我快速解決。

在這裏創建一個自定義的Button類。

class classname: UIButton { var imageName: String? }

坐你的故事板類從UIButton的改變類名

在你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 
} 

現在讓我們看一下你的tapbutton實現

FUNC tapFunction(發件人: classname){

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類,並在該模型中使用UIImage varaivals,該類將保存單元格的當前圖像。單擊按鈕操作時,只需使用當前圖像更改UIImage變量。