单击UITableviewCells时更改UIButton标题

时间:2015-05-02 09:58:46

标签: ios uitableview swift uibutton

我用VK Api制作简单的玩家。我想做这个。

  

当我点击第一首曲目然后第二次将第一个按钮标题“停止”更改为“自动播放”。

怎么做?

我的播放/停止按钮动作。

func playAction(sender: UIButton!) {
    if sender.currentTitle == "P" {
        let track = dataOfTracks[sender.tag] as trackDoc
        let url = NSURL(string: track.data.url)

        player = AVPlayer(URL: url!)
        player.play()
        sender.setTitle("S", forState: UIControlState.Normal)
    } else if sender.currentTitle == "S" {
        sender.setTitle("P", forState: UIControlState.Normal)
        player.pause()
    }
}

enter image description here

2 个答案:

答案 0 :(得分:0)

编辑代码详情:

  1. 每次按钮都不会重新分配。它必须是SongsTableViewCell类的另一个出口(与标签相同)。并从Interface Builder中设置目标/操作(在#34; func cellSongClicked"和ctrl-drag from IB)前添加@IBAction

    2.为您的班级添加以下属性:private var currentSong : Int? 3)方法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
    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
    

    }

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

答案 1 :(得分:0)

您不必每次都更改按钮的标题。你可以使用这样的东西:

func viewDidLoad {
    playButton.setTitle("Play", forState: UIControlState.Normal)
    playButton.setTitle("Stop", forState: UIControlState.Selected)
}

然后在您的回调方法中,您只需更改按钮的状态,文本也会更新:

func playAction(sender: UIButton!) {
    // Selected means the content is playing
    if !sender.selected {
        let track = dataOfTracks[sender.tag] as trackDoc
        let url = NSURL(string: track.data.url)

        player = AVPlayer(URL: url!)
        player.play()
    } else {
        player.pause()
    }
    // update the state of the button
    sender.selected = !sender.selected
}

我认为这是一个更干净的解决方案,因为你不再比较字符串来做出决定而你实际上是使用按钮的二进制状态来跟踪你内容的当前状态(也是二进制的,无论是否打球。)

如果您有更多问题,我会非常乐意为您提供进一步的帮助,请告诉我。