【问题标题】:Change the UIButton title when click on UITableviewCells单击 UITableviewCells 时更改 UIButton 标题
【发布时间】:2015-05-02 09:58:46
【问题描述】:

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

【问题讨论】:

  • 不明白你想要什么?单击任何单元格时是否要更改按钮的标题?请详细说明
  • @chiragshah 当我在第一个带有轨道的单元格中单击播放时,然后第二个,我需要将第一个单元格按钮标题更改为默认值(“播放”)
  • 为此,您必须从单元格中获取该按钮并更改该按钮的标题。我做到了目标 c 但不是迅速
  • @chiragshah 发给我 ojb-c 代码 dr_temka@icloud.com
  • @chiragshah 这是我现在的问题。我如何在 Objective-C 中做到这一点?

标签: ios uitableview swift uibutton


【解决方案1】:

编辑代码细节:

  1. 不要每次按钮都重新分配。它必须是 SongsTableViewCell 类的另一个出口(与标签相同)。并从 Interface Builder 设置目标/动作(在“func cellSongClicked”前面添加@IBAction,并从 IB 中按 ctrl-drag)

    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
    

    }

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

【讨论】:

    【解决方案2】:

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

    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
    }
    

    我认为这是一个更干净的解决方案,因为您不再比较字符串来做出决定,并且您实际上是在使用按钮的二进制状态来跟踪内容的当前状态(这也是二进制的,播放或不播放)。

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

    【讨论】:

    • 您能说得更具体一点吗?什么不起作用?
    • 我为我的问题添加了 gif
    • 如何设置按钮的宽度?这似乎只是一个布局问题。只需增加宽度,Stop 也可以显示,它应该可以工作
    • 诺诺诺。单击下一首曲目时,我需要将标题上一个按钮设置为默认值
    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    相关资源
    最近更新 更多