[英]How to change previously pressed UIButton label in UITableViewCell in OBJECTIVE C?
[英]How to change previously pressed UIButton label in UITableViewCell?
我有帶標簽的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)按下按鈕時,刷新當前播放的歌曲單元格,並調用它們的新播放的歌曲單元格“ reloadRowsAtIndexPaths”
希望這可以幫助
編輯代碼的詳細信息:
1)不要每次都重新分配按鈕。 它必須是SongsTableViewCell類的另一個出口(與標簽相同)。 並從“界面生成器”中設置目標/操作(在“ func cellSongClicked”前面添加@IBAction,並從IB中添加ctrl-drag)
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(使用temp boolean stopCurrent)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.