找到你要的答案

Q:Change the UIButton title when click on UITableviewCells

Q:改变UIButton标题时,点击行

I make simple player with VK Api. I want to do this.

When I clicked first track and then second change first button title "stop" to "play" automatically.

How to do it?

My play/stop button action.

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

我和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()
    }
}

answer1: 回答1:

EDIT CODE DETAILS:

  1. don't reallocate every time the button. It must be another outlet of the SongsTableViewCell class (same as the label). And set the target/action from the Interface Builder (add @IBAction in front of "func cellSongClicked" and ctrl-drag from IB)

    2.add the following property to your class:private var currentSong : Int? 3) method 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) And the action:

@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. 不要把每一次的按钮。它必须是另一个出口的songstableviewcell类(如标签)。设定目标/行动从界面生成器(加“在”功能cellsongclicked”和Ctrl键拖动IB前ibaction)

    2.add the following property to your class:private var currentSong : Int? 3) method cellForRowAtIndexPath:

    重载函数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)
}
answer2: 回答2:

You don't have to change the title of the button every time. You could use something like this:

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

Then in your callback method you just change the state of the button and the text will get updated too:

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
}

I consider this to be a more clean solution since you are not comparing strings anymore to make your decision and you are actually using the binary state of the button to keep track of the current state of your content(which is also binary, either playing or not).

If you have more questions, I would be more than happy to help you further, just let me know.

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

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
}

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

如果你有更多的问题,我会很乐意帮助你进一步,让我知道。

ios  uitableview  swift  uibutton