2014-12-01 71 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

相关问题