I have a custom cell in my table view which contain a button and name of audio file. when i am tapping in button the relevant audio is playing and when tapping again it audio stops. My problem is when i am tapping the same button it is ok but when i am tapping in any button different then current cell then my process is not working. Can any one show me the right direction to do this . My code is below . Thanks in advance..
var isAudioPlaying = false
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return designCellAtIndexPath(indexPath)
}
private func designCellAtIndexPath(indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = NSBundle.mainBundle().loadNibNamed("ExtendedDesign", owner: self, options: nil)[4] as! UITableViewCell
cell.frame = CGRectMake(0.0, 0.0, UIScreen.mainScreen().bounds.size.width, cell.bounds.height)
cell.contentView.frame = cell.bounds
cell.selectionStyle = UITableViewCellSelectionStyle.None
btnPlayPause = cell.viewWithTag(1) as! subclassedUIButton
btnPlayPause.addTarget(self, action: "btnPlayPauseTapped:", forControlEvents: .TouchUpInside)
btnPlayPause.urlString = arrAttractionList[indexPath.row].audio.url
let lblAttractionName = cell.viewWithTag(2) as! UILabel
lblAttractionName.text = arrAttractionList![indexPath.row].name
return cell
}
My Button Methode is:
func btnPlayPauseTapped(sender: subclassedUIButton) {
if !isAudioPlaying {
isAudioPlaying = true
sender.setImage(UIImage(named: "Button_Pause"), forState: .Normal)
AudioHandler.sharedManager().playAudio(sender.urlString)
} else {
isAudioPlaying = false
sender.setImage(UIImage(named: "Button_Play_Menu.png"), forState: .Normal)
AudioHandler.sharedManager().audioPlayer?.pause()
}
}
// Custome class for UIButton
class subclassedUIButton: UIButton {
var indexPath: Int?
var urlString: String?
}
You have to manage other flag for your current cell's audio you have to play. Set current cell index and manage that.
let cell: UITableViewCell = NSBundle.mainBundle().loadNibNamed("ExtendedDesign", owner: self, options: nil)[4] as! UITableViewCell
This is not the correct way to add a cell to a table view. You should call registerNib(_:forCellReuseIdentifier:) (usually in the viewDidLoad() method) and then dequeueReusableCellWithIdentifier(_:forIndexPath:) in your cellForRowAtIndexPath(_:) delegate method.
The registerNib(_:forCellReuseIdentifier:) isn't needed if you design your cell with a template in the storyboard.
But you really need to use table views the way they were designed to be used. What you have now might seem to work, but it's going to have problems and make your life miserable if you don't use table views the way they were intended to be used.
Also, you don't want to be referencing your subviews with viewWithTag(_:). You want to wire up proper IBOutlets in Interface Builder. Or at least creating properties in your UITableView subclass to access the subviews.
Finally I did it here is my code to do this. First Declare Two variables in class:
var currentCellIndex :int = -1
var previousCellIndex : int = -1
func btnPlayPauseTapped(sender: subclassedUIButton) {
currentCellIndex = sender.indexPath!
if currentCellIndex == previousCellIndex {
if !isAudioPlaying {
isAudioPlaying = true
AudioHandler.sharedManager().stopAudioPlayer()
sender.setImage(UIImage(named: "Button_Play_Menu.png"), forState: .Normal)
} else {
isAudioPlaying = false
AudioHandler.sharedManager().playAudio(sender.urlString)
sender.setImage(UIImage(named: "Button_Pause"), forState: .Normal)
}
} else {
let indexPath = NSIndexPath(forRow: previousCellIndex, inSection: 0)
tblAttractinList.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
isAudioPlaying = false
sender.setImage(UIImage(named: "Button_Pause"), forState: .Normal)
AudioHandler.sharedManager().playAudio(sender.urlString)
previousCellIndex = sender.indexPath!
}
}
Thank You all of you for your help! :)
Related
I am still new to swift and I would ask you for advice. Thank you in advance and sorry for my bad English.
My goal is:
User tap edit button in the table's row. UITextField appears instead cell. After entering value and pressing Return key UITextField disappears again and cell is recalculated.
editButton pressed -> hide priceCell & show UITextField & show keyboard & start editing/entering value (blinking cursor) -> stop editing/entering value execute by pressing Return key -> hide UITextField & shows priceCell & save entered value into array & reload edited row
I use this answer as starting blueprint.
I would like to also use .decimalPad keyboard to easier entering numeric value and limit user to use only numbers (and decimal point), but this exclude use Return key as stop editing, am I right?
I found this possible solution, but it seems to me complex for my problem...
my ViewController:
import UIKit
class PortfolioViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, PortfolioCellDelegate {
let getData = GetData()
...
override func viewDidLoad() {
super.viewDidLoad()
cellTableView.delegate = self
cellTableView.dataSource = self
cellTableView.register(UINib(nibName: "PortfolioCell", bundle: nil), forCellReuseIdentifier: "portfolioCell")
self.currencyControl.selectedSegmentIndex = MyVariables.currencyControlSelected
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let coinCell = tableView.dequeueReusableCell(withIdentifier: "portfolioCell", for: indexPath) as! PortfolioCell
...
coinCell.delegate = self
return coinCell
}
...
func portfolioButtonPressed(coinCell: PortfolioCell) {
let indexPath = self.cellTableView.indexPathForRow(at: coinCell.center)!
let selectedCell = cellTableView.cellForRow(at: indexPath) as! PortfolioCell
selectedCell.priceCell.isHidden = true
selectedCell.textCell.isHidden = false
selectedCell.textCell.delegate = self
func textFieldDidEndEditing(textField: UITextField) {
let owned: Double = Double(textField.text!)!
if owned >= 0 {
MyVariables.dataArray[indexPath.row].ownedCell = owned
} else {
MyVariables.dataArray[indexPath.row].ownedCell = 0.00
}
selectedCell.priceCell.isHidden = false
selectedCell.textCell.isHidden = true
self.cellTableView.reloadData()
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
selectedCell.textCell.resignFirstResponder()
return true
}
}
...
}
my custom cell:
import UIKit
protocol PortfolioCellDelegate {
func portfolioButtonPressed(coinCell: PortfolioCell)
}
class PortfolioCell: UITableViewCell {
var delegate: PortfolioCellDelegate?
...
#IBAction func editCellPressed(_ sender: UIButton) {
delegate?.portfolioButtonPressed(coinCell: self)
}
...
}
For now when button is pressed proper UITextField shows, but don't dismiss after Return key is pressed.
Or should I change it completely and use tap gestures?
To end editing in any kind of scrollView, simply use this
cellTableView.keyboardDismissMode = .onDrag
or
cellTableView.keyboardDismissMode = .interactive
It will hide keyboard when you interact with the tableView
For number keypad you can add toolbar as a textField's inputAccessoryView. On toolbar add cancel button to dismiss keyboard.
There is two way to go:
1.) Delegate
2.) IQKeyboardManager
1.)
Use UITextFieldDelegate
There is one particular callback named "textFieldShouldEndEditing"
In this method, return true.
2.)
User the IQKeyboardManager one liner library. This library manages all the TextFields and scrollviews automatically. You activate it with one line in AppDelegate so it's easy to use.
https://github.com/hackiftekhar/IQKeyboardManager
Working but not as sleek as want it to be and also I was not capable to make IQKeyboardManager works so I did use inputAccessoryView.
custom cell:
import UIKit
protocol PortfolioCellDelegate {
func portfolioButtonPressed(didSelect coinCell: PortfolioCell)
func portfolioButtonPressed(coinCell:PortfolioCell, editingChangedInTextCell newValue:String)
}
class PortfolioCell: UITableViewCell, UITextFieldDelegate {
var delegate: PortfolioCellDelegate?
...
#IBAction func editCellPressed(_ sender: UIButton) {
textCell.becomeFirstResponder()
delegate?.portfolioButtonPressed(didSelect: self)
}
#IBAction func textCellValueChanged(_ sender: UITextField) {
if (sender.text?.isEmpty)! {
delegate?.portfolioButtonPressed(coinCell: self, editingChangedInTextCell: "XXX")
} else {
let text = sender.text
delegate?.portfolioButtonPressed(coinCell: self, editingChangedInTextCell: text!)
}
}
override func awakeFromNib() {
super.awakeFromNib()
self.textCell.delegate = self
let flexiableSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.doneButtonAction))
let toolBar:UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: 35))
toolBar.barTintColor = UIColor(red:0.15, green:0.69, blue:0.75, alpha:1.0)
toolBar.tintColor = UIColor(red:0.93, green:0.93, blue:0.93, alpha:1.0)
toolBar.setItems([flexiableSpace, doneButton], animated: false)
textCell.inputAccessoryView = toolBar
textCell.keyboardType = UIKeyboardType.decimalPad
}
#objc func doneButtonAction() {
textCell.endEditing(true)
}
...
}
ViewController:
import UIKit
class PortfolioViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, PortfolioCellDelegate {
let getData = GetData()
...
override func viewDidLoad() {
super.viewDidLoad()
cellTableView.delegate = self
cellTableView.dataSource = self
cellTableView.register(UINib(nibName: "PortfolioCell", bundle: nil), forCellReuseIdentifier: "portfolioCell")
self.currencyControl.selectedSegmentIndex = MyVariables.currencyControlSelected
getData.delegate = self
timeStampLabel.text = MyVariables.timeStamp
}
override func viewDidAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.cellTableView.reloadData()
self.currencyControl.selectedSegmentIndex = MyVariables.currencyControlSelected
self.timeStampLabel.text = MyVariables.timeStamp
}
//MARK: - tableView
/***************************************************************/
...
func portfolioButtonPressed(coinCell: PortfolioCell, editingChangedInTextCell newValue: String) {
let indexPath = self.cellTableView.indexPathForRow(at: coinCell.center)!
let selectedCell = cellTableView.cellForRow(at: indexPath) as! PortfolioCell
selectedCell.priceCell.isHidden = false
selectedCell.textCell.isHidden = true
if newValue != "XXX" {
let owned: Double = Double(newValue)!
MyVariables.dataArray[indexPath.row].ownedCell = owned
}
selectedCell.priceCell.isHidden = false
selectedCell.textCell.isHidden = true
self.cellTableView.reloadRows(at: [indexPath], with: .automatic)
}
func portfolioButtonPressed(didSelect coinCell: PortfolioCell) {
let indexPath = self.cellTableView.indexPathForRow(at: coinCell.center)!
let selectedCell = cellTableView.cellForRow(at: indexPath) as! PortfolioCell
selectedCell.priceCell.isHidden = true
selectedCell.textCell.isHidden = false
}
...
}
It's easy: You should select that table view cell, then enable User Interaction Enabled in the attribute inspector.
I have a UITableView with a button in that's toggled depending on whether a user 'favorites' a post or not. Everything works well, except for when the table view is scrolled, the button changes. Here's my code:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let feed = self.feed else {
return 0
}
return feed.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if feed!.count > 9 {
if indexPath.row == feed!.count - 1 {
self.loadMorePosts()
}
}
if hasImageAtIndexPath(indexPath) {
return imageCellAtIndexPath(indexPath)
} else {
return basicCellAtIndexPath(indexPath)
}
}
func hasImageAtIndexPath(indexPath:NSIndexPath) -> Bool {
let post = self.feed?[indexPath.row]
if post?.image?.isEmpty == false {
return true
}
return false
}
func imageCellAtIndexPath(indexPath:NSIndexPath) -> PostCellImage {
let cell:PostCellImage = self.tableView.dequeueReusableCellWithIdentifier("imageCell", forIndexPath: indexPath) as! PostCellImage
if let post = self.feed?[indexPath.row] {
let likedPost = post.hasFavorited
if likedPost == true {
if let favoriteCount = post.favoriteCount {
let count = String(favoriteCount)
cell.likeButton.setTitle(count, forState: .Normal)
cell.likeButton.setImage(UIImage(named: "liked"), forState: .Normal)
cell.likeButton.setTitleColor(UIColorFromRGB("A61224"), forState: .Normal)
cell.likeButton.addTarget(self, action: "unfavoritePost:", forControlEvents: UIControlEvents.TouchUpInside)
cell.likeButton.tag = post.id!
}
} else {
if let favoriteCount = post.favoriteCount {
let count = String(favoriteCount)
cell.likeButton.setTitle(count, forState: .Normal)
cell.likeButton.addTarget(self, action: "favoritePost:", forControlEvents: UIControlEvents.TouchUpInside)
cell.likeButton.tag = post.id!
}
}
}
return cell
}
Favorited Posts Array
var favoritedPosts = [Int]()
Table View
if let likedPost = post.hasFavorited {
if likedPost == true {
self.favoritedPosts.append(indexPath.row)
print(self.favoritedPosts)
}
}
if self.favoritedPosts.contains(indexPath.row) {
let count = String(post.favoriteCount)
cell.likeButton.setTitle(count, forState: .Normal)
cell.likeButton.setImage(UIImage(named: "liked"), forState: .Normal)
cell.likeButton.setTitleColor(UIColorFromRGB("A61224"), forState: .Normal)
cell.likeButton.addTarget(self, action: "unfavoritePost:", forControlEvents: UIControlEvents.TouchUpInside)
cell.likeButton.tag = post.id!
} else {
let count = String(post.favoriteCount)
cell.likeButton.setTitle(count, forState: .Normal)
cell.likeButton.addTarget(self, action: "favoritePost:", forControlEvents: UIControlEvents.TouchUpInside)
cell.likeButton.tag = post.id!
}
In your UITableViewCell PostCellImage subclass you should override prepeareForReuse function - to turn cell to default mode.
Swift:
override func prepareForReuse() {
super.prepareForReuse()
//set cell to initial state here
//set like button to initial state - title, font, color, etc.
}
This might be cased by the table view cell reuse. It turns out that your code set the likeButton.image when the post is a favorited post but did not remove the image when the post is not a favorited one. So when the first time each cell is loaded into tableView every thing works fine. However, when scrolling the tableView, when those cells with favorite image set moves out of the screen area, they will be reused for the cell scrolling in. Thus if this kind of cell is reused by a post that is even not favorited, the image will still be there.
There is a prepareForReuse method for UITableViewCell, it gives you the chance to reset the contents before a cell is being reused.
You need to completely reset the appearance for normal case here.
Reason is you can't be sure which state of the button you get when
cells are reused.
When you receive a dequeued cell, it might have a
button with 'liked'/'not-liked' state.
Completing the appearance with two commented out lines in the else clause will resolve your problem.
if self.favoritedPosts.contains(indexPath.row) {
let count = String(post.favoriteCount)
cell.likeButton.setTitle(count, forState: .Normal)
cell.likeButton.setImage(UIImage(named: "liked"), forState: .Normal)
cell.likeButton.setTitleColor(UIColorFromRGB("A61224"), forState: .Normal)
cell.likeButton.addTarget(self, action: "unfavoritePost:", forControlEvents: UIControlEvents.TouchUpInside)
cell.likeButton.tag = post.id!
} else {
let count = String(post.favoriteCount)
cell.likeButton.setTitle(count, forState: .Normal)
// Uncomment these two lines and add proper values for image / color to resolve your problem
// cell.likeButton.setImage(UIImage(named: "not-liked-yet"), forState: .Normal)
// cell.likeButton.setTitleColor(UIColorFromRGB("A67832"), forState: .Normal)
cell.likeButton.addTarget(self, action: "favoritePost:", forControlEvents: UIControlEvents.TouchUpInside)
cell.likeButton.tag = post.id!
}
Hope this helps.
You can try prepareForReuse or try to set unlike image in the case likedPost == false
I am working on a "to do" app in Swift and using Firebase as my backend.
On the first tap on my checkbox to signal that a task has been done, the UI updates and the variable should become true (a checkmark appears) but firebase and the local instance of the bool value for that item do not update to false. After a second tap, the UI continues to have normal functionality (the checkmark disappears); but firebase and the local instance both update to true. Following taps from then on are reversed (True for no checkmark and false for checkmark). When I stop the simulator in Xcode and re-run, the values and UI that load in are correct. It is not until I try and tap on the checkmark that I get the incorrect functionality again. Firebase only updates after the second tap and change in UI. I have include just the code that pertains to the checkbox. I think the problem happens in the doneHit function but I am not quite sure why its happening.
Please help. If there is an easier way to go about this, that would be helpful too.
protocol TaskCellDelegate {
func doneHit(cell : TaskCell)
}
class TaskCell : UITableViewCell {
var delegate : TaskCellDelegate?
#IBOutlet weak var label: UILabel!
#IBOutlet weak var checkBox: CheckBox!
#IBOutlet weak var detailLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
checkBox.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
}
func buttonClicked(sender:UIButton) {
delegate?.doneHit(self)
}
}
class CheckBox: UIButton {
//images
let checkedImage = UIImage(named: "checkedbox")
let unCheckedImage = UIImage(named: "uncheckedbox")
//bool propety
var isChecked:Bool = false{
didSet {
if isChecked == true{
self.setImage(checkedImage, forState: .Normal)
}
else {
self.setImage(unCheckedImage, forState: .Normal)
}
}
}
override func awakeFromNib() {
self.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
}
func buttonClicked(sender:UIButton) {
if(sender == self){
if isChecked == false {
isChecked = true
} else {
isChecked = false
}
}
}
}
func doneHit(cell:TaskCell) {
if let ip = tableView.indexPathForCell(cell) {
var task = tasks[ip.row]
task.done = cell.checkBox.isChecked
if task.done == true {
task.completedBy = "Completed by: \(self.user)"
cell.label.textColor = UIColor.grayColor()
}
else {
task.completedBy = ""
cell.label.textColor = UIColor.blackColor()
}
let taskNameRef = self.ref.childByAppendingPath("tasks/\(task.title)")
let completedByData = ["completedBy": "\(self.user)"]
let doneData = ["done": task.done]
taskNameRef.updateChildValues(completedByData)
taskNameRef.updateChildValues(doneData)
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("TaskCell", forIndexPath: indexPath) as! TaskCell
// Configure the cell...
cell.selectionStyle = .None
let idx = tasks[indexPath.row]
if let label = cell.viewWithTag(1) as? UILabel {
label.text = idx.title
if idx.done == true {
label.textColor = UIColor.grayColor()
} else {
label.textColor = UIColor.blackColor()
}
if let checkBox = cell.viewWithTag(2) as? CheckBox {
checkBox.isChecked = idx.done
}
if let userCompleted = cell.viewWithTag(3) as? UILabel {
if idx.done == true {
userCompleted.text = "Completed By: \(idx.completedBy)"
}
else {
userCompleted.text = ""
}
}
}
print("Task.done is: \(idx.done)")
print("isChecked is:\(cell.checkBox.isChecked)")
cell.delegate = self
return cell
}
Figured it out myself after 3 days. I scrapped the Checkbox class and turned the checkbox into a UIImageView and added a gesture recognizer. The rest was just moving the Checkbox logic into the TaskTVC class under the doneHit method.
I have a slider inside a table view cell and I'm trying to set the text of a label to the value of the slider. However, the text isn't setting to the changed slider values. What am I doing wrong?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
if indexPath.section == 0 {
let slider = UISlider(frame: CGRectMake(0, 0, 320, 44))
slider.userInteractionEnabled = true
slider.minimumValue = 1
slider.maximumValue = 100
slider.value = 50
slider.continuous = true
cell.addSubview(slider)
println(cell.bounds)
let label = UILabel(frame: CGRectMake(260, -20, 100, 20))
var theValue = Int(slider.value)
label.text = "\(theValue)" + "mi."
cell.addSubview(label)
}
You can do it as you have using some tricks, first of all you need to set the target for the UISlider as some as suggested, the other point is get the selected row, which you can achieve saving in the tag property the indexPath.row like in the following code:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
let slider = UISlider(frame: CGRectMake(0, 0, 100, 44))
slider.userInteractionEnabled = true
slider.minimumValue = 1
slider.maximumValue = 100
slider.value = 50
slider.continuous = true
// set the target to respond to changes.
slider.addTarget(self, action: "sliderValueChanged:", forControlEvents: UIControlEvents.ValueChanged)
// save the indexPath.row
slider.tag = indexPath.row
cell.addSubview(slider)
let label = UILabel(frame: CGRectMake(105, 0, 100, 20))
var theValue = Int(slider.value)
label.text = "\(theValue)" + "mi."
cell.addSubview(label)
return cell
}
The another problem is set the UILabel text , which you can achieve by getting the exactly view inside the cell, like in the following code:
func sliderValueChanged(sender: AnyObject) {
var slider = sender as! UISlider
let cell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: slider.tag, inSection: 0)) as UITableViewCell!
// If you know the exactly index
// var label = cell.subviews[4] as! UILabel
// label.text = "\(slider.value)"
// assuming you have only one `UILabel` inside your cell
for view in cell.subviews {
if let label = view as? UILabel {
label.text = "\(slider.value)"
}
}
}
In the above code I only set the exactly number inside the UITableViewCell for purpose of show the way, but the strongly recommended way is iterate through all the UIView and find your UILabel like in the method proposed in this question Find UILabel in UIView in Swift because the number can change.
As some people said before, is more easy set a Custom Cell all through Interface Builder, but it's up to you.
I hope this help you.
Generally you should add an action to your slider :
slider.addTarget(self, action: "sliderValueDidChange:", forControlEvents: .ValueChanged)
func sliderValueDidChange(sender:UISlider!)
{
println("value--\(sender.value)")
}
as described here : http://www.apptuitions.com/programmatically-creating-uiview-uislider-uiswitch-using-swift/
** As you need to change label and use slider's value in any cell, separately, You have to use custom TableViewCell class and define your slider, label and action in it, easily.
** MY RECOMMENDATION: Use storyBoard and then easily design your view and use customized TableViewCell class.
a sample table view cell class for you ( similar to one in my own project ) :
class CustomCell: UITableViewCell {
#IBOutlet weak var slider: UISlider!
#IBOutlet weak var label: UILabel!
#IBAction func sliderChanged(sender: AnyObject) {
// set label text to slider.value here
}
}
You are missing action on slider drag.
slider.addTarget(self, action: "sliderValueDidChange", forControlEvents: .ValueChanged)
func sliderValueDidChange(sender:UISlider!){
println("value--\(sender.value)")
label.text = String(sender.value)
}
You can use slider.superview or store index in tag of the slider in order to get the reference to cell to which slider belongs and access label.
Better solution will be to create custom class for the cell.
Add this in your cellForRowAtIndexPath function:
slider.addTarget(self, action: "sliderValueChanged:", forControlEvents: .ValueChanged)
label.tag = 1
You can use this to update the label:
func sliderValueChanged(sender: AnyObject)
{
var position: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
if let indexPath = self.tableView.indexPathForRowAtPoint(position) {
//get your cell here
let cell = tableView.cellForRowAtIndexPath(indexPath)
//update label
for view in cell.subviews {
if let label = view as? UILabel {
if label.tag == 1 {
label.text! = "\((sender as! UISlider).value)"
}
}
}
}
}
OR
You can do what others have mentioned and create a Custom UITableViewCell, add a slider and a label, and create IBOutlets for them.
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()
}
}
EDIT CODE DETAILS:
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)
}
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.