我有UITableView
標籤和一個按鈕來顯示音頻列表。如何更改UITableViewCell中先前按下的UIButton標籤?
一旦按下標有「播放」的按鈕,它將變爲「停止」並播放音頻,如果在其他單元格中按下另一個按鈕,則上一個單元格按鈕應將其標籤更改爲「播放」。
現在,我在這裏的問題是如何將前一個單元格按鈕更改回「播放」?
標籤是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)
}
}
感謝
謝謝@valfer,我有點困惑在這裏,請你調整我的代碼在哪裏把你的代碼... – 2014-12-01 18:37:14
我同意 - 這是最好的解決方案。但是,OP目前正在重用你的單元格*和*爲每次調用cellForRowAtIndexPath添加新的按鈕作爲子視圖,所以這是你必須警惕的,特別是當你走這條路線並強制重新加載時。 – 2014-12-01 18:38:32
將代碼詳細信息添加到我的答案 – valfer 2014-12-01 19:10:03