如何在UITableViewCell中更改以前按下的UIButton标签



我有标签和显示音频列表的按钮UITableView

按下标有"播放">

的按钮后,它将变为"停止"并播放音频,如果在另一个单元格中按下另一个按钮,则上一个单元格按钮应将其标签更改为"播放"。

现在,我在这里遇到的问题是如何将以前的单元格按钮改回"播放"?

标签是UITableViewCell中的插座,UIButton以编程方式添加cellForRowAtIndexPath

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

谢谢

我会使用这个策略:

1( 在类的属性中维护包含当前播放歌曲的单元格的索引路径(您知道"cellSongClicked"操作中 UIButton 标签中的新 indexPath(

2(在"cellForRowAtIndexPath"中,将按钮的标题设置为"播放"或"停止",具体取决于当前播放歌曲的单元格

3(按下按钮时,刷新当前播放的歌曲单元格和新的播放歌曲单元格,调用它们"重新加载行在索引路径">

希望这有帮助

编辑代码详细信息:

1(不要每次都重新分配按钮。它必须是 SongsTableViewCell 类的另一个出口(与标签相同(。并从界面构建器中设置目标/操作(在"func cellSongClicked"前面添加@IBAction并从IB中按住ctrl并拖动(

2( 将以下属性添加到类中:

private var currentSong : Int?

3( 方法 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 属性设置为 nil(使用临时布尔值 stopCurrent(。

最新更新