2014-12-01 22 views
1

我有UITableView標籤和一個按鈕來顯示音頻列表。如何更改UITableViewCell中先前按下的UIButton標籤?

一旦按下標有「播放」的按鈕,它將變爲「停止」並播放音頻,如果在其他單元格中按下另一個按鈕,則上一個單元格按鈕應將其標籤更改爲「播放」。

現在,我在這裏的問題是如何將前一個單元格按鈕更改回「播放」?

標籤是UITableViewCell網點和UIButtoncellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("SongTitleCell", forIndexPath: indexPath) as SongsTableViewCell 

    let songDic : NSDictionary = arrSongs.objectAtIndex(indexPath.row) as NSDictionary 

    cell.lblSongTitle.text = songDic.objectForKey("SongTitle") as? String 


    btnPlayPause = UIButton(frame : CGRectMake(13,2,40,40)) 
    btnPlayPause.tag = indexPath.row 
    btnPlayPause.setTitle("Play", forState: UIControlState.Normal) 
    btnPlayPause.addTarget(self, action: "cellSongClicked:", forControlEvents: UIControlEvents.TouchUpInside) 

    cell.contentView.addSubview(btnPlayPause) 

    return cell 
} 

此按鈕的動作編程補充說:

func cellSongClicked (sender : AnyObject){ 
    var remote = GetSongData() 
    remote.delegate = self 

    var btnCurrentPressed = sender as UIButton 

    //Play Selected Song 
    let songDic : NSDictionary = arrSongs.objectAtIndex(btnCurrentPressed.tag) as NSDictionary 

    if (btnCurrentPressed.currentTitle == "Play") { 

     remote.PlaySelectedSong(songDic.objectForKey("SongURL") as String!) 
     btnCurrentPressed.setTitle("Stop", forState: UIControlState.Normal) 

    } else { 
     playerContoller.stop() 
     btnCurrentPressed.setTitle("Play", forState: UIControlState.Normal) 
    } 

} 

感謝

回答

2

我會用這個策略:

1)保留包含當前播放歌曲的單元格的索引路徑(您知道在「cellSongClicked」動作中來自UIButton標籤的新索引路徑)

2)在「cellForRowAtIndexPath」集中根據當前播放歌曲的單元格,按鈕的標題爲「播放」或「停止」

3)按下按鈕時刷新當前播放歌曲單元格和新的播放歌曲單元格以調用它們「reloadRowsAtIndexPaths」

希望這會有所幫助

編輯代碼詳情:

1)不要每次都重新分配按鈕。它必須是SongsTableViewCell類的另一個出口(與標籤相同)。

private var currentSong : Int? 

3:並設置在界面生成器的目標/行動

2)以下屬性添加到類(在「FUNC cellSongClicked」,並從IB按Ctrl鍵前面加@IBAction) )方法的cellForRowAtIndexPath:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("SongTitleCell", forIndexPath: indexPath) as SongsTableViewCell 

    let songDic : NSDictionary = arrSongs.objectAtIndex(indexPath.row) as NSDictionary 

    cell.lblSongTitle.text = songDic.objectForKey("SongTitle") as? String 
    cell.btnPlayPause.tag = indexPath.row 

    var title : String 
    if let _currentSong = currentSong { 
     title = indexPath.row == _currentSong ? "Stop" : "Play" 
    } else { 
     title = "Play" 
    } 
    cell.btnPlayPause.setTitle(title, forState: UIControlState.Normal) 

    return cell 
} 

4)和所述的操作:

@IBAction func cellSongClicked (sender : AnyObject){ 
    var remote = GetSongData() 
    remote.delegate = self 

    var btnCurrentPressed = sender as UIButton 

    //Play Selected Song 
    let songDic : NSDictionary = arrSongs.objectAtIndex(btnCurrentPressed.tag) as NSDictionary 

    var rowsToReload = [NSIndexPath]() 
    var stopCurrent = false 
    if let _currentSong = currentSong { 
     if btnCurrentPressed.tag == _currentSong { 
      stopCurrent = true 
     } else { 
      rowsToReload.append(NSIndexPath(forRow: _currentSong, inSection:0)) 
     } 
    } 
    rowsToReload.append(NSIndexPath(forRow: btnCurrentPressed.tag, inSection:0)) 
    currentSong = stopCurrent ? nil : btnCurrentPressed.tag 
    self.tableView.reloadRowsAtIndexPaths(rowsToReload, withRowAnimation: .None) 
} 

編輯:添加當前歌曲停止

檢查用戶是否正在點擊當前播放按鈕(if btnCurrentPressed.tag == _currentSong)。在這種情況下,不要重新加載該行(否則您將重新加載它兩次),並將currentSong屬性設置爲零(使用temp boolean stopCurrent)。

+0

謝謝@valfer,我有點困惑在這裏,請你調整我的代碼在哪裏把你的代碼... – 2014-12-01 18:37:14

+0

我同意 - 這是最好的解決方案。但是,OP目前正在重用你的單元格*和*爲每次調用cellForRowAtIndexPath添加新的按鈕作爲子視圖,所以這是你必須警惕的,特別是當你走這條路線並強制重新加載時。 – 2014-12-01 18:38:32

+0

將代碼詳細信息添加到我的答案 – valfer 2014-12-01 19:10:03

相關問題