Reputation: 57
I have a custom cell setup with an add and subtract button, and a quantity label. I'd like a label in the view controller to update with the price shown in the cell.
I've scoured SE and the google machine, but all examples are for Objective-C and are satisfied with passing the indexPath.row
to the selector. I've managed to pass the row in which the add button was tapped to the selector method, however I can't access the objects in the cell, e.g cell.itemNameLabel.text
I've tried implementing in the selector method something similar to the objective-c: with no joy.
UITableViewCell* cell = (UITableViewCell*)[sender superview];
NSIndexPath* indexPath = [myTableView indexPathForCell:cell];
Here is my setup:
Custom Cell:
class ProductListTableViewCell: UITableViewCell {
@IBOutlet var itemNameLabel: UILabel! = UILabel()
@IBOutlet weak var itemImage: UIImageView! = UIImageView()
@IBOutlet var itemPriceLabel: UILabel! = UILabel()
@IBOutlet weak var itemQty: UILabel!
@IBOutlet weak var addItem: UIButton! = UIButton()
Main view controller:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:ProductListTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as ProductListTableViewCell
let drink:PFObject = self.productListData.objectAtIndex(indexPath.row) as PFObject
cell.itemImage.layer.cornerRadius = cell.itemImage.frame.size.width / 2
cell.itemImage.clipsToBounds = true
cell.addItem.tag = indexPath.row
cell.addItem.addTarget(self, action:("cellAddItemAction:"), forControlEvents: UIControlEvents.TouchUpInside)
cell.itemNameLabel.text = drink.objectForKey("name") as? NSString
cell.calorieLabel.text = drink.objectForKey("calories") as? NSString
cell.itemPriceLabel.text = drink.objectForKey("price") as? NSString
cell.itemImage.alpha = 1
let itemImage = drink["image"] as PFFile
itemImage.getDataInBackgroundWithBlock{
(imageData: NSData!, error: NSError!) -> Void in
if error == nil {
let image = UIImage(data: imageData)
cell.itemImage.image = image
}
}
}
)
return cell
}
Selector method:
func cellAddItemAction(sender: UIButton){
println("pressed \(sender.tag)")
}
This works but I need to make changes to the objects in the cell, such as cell.itemQty.text.
Hope this is clear, i'm quite new to programming so please be patient with my terminology, I know its not all right!! Thanks for helping.
Upvotes: 0
Views: 1425
Reputation: 57
OK, so I went about it slightly differently. This is a prototype app and needs only to appear to work. Heres what I did if it helps anyone though.
Add a tag to the button being pressed in cellForRowAtIndexPath
:
cell.addItem.tag = indexPath.row
Then when adding the target, instead of using Self, use the cell:
cell.addItem.addTarget(cell, action:("cellAddItemAction:"), forControlEvents: UIControlEvents.TouchUpInside)
Declare a global variable in the viewController:
let priceUpdateKey = "com.ordr.priceUpdateKey"
Now write the function inside the cell class, and add the NSNotification method, sending your data in the userInfo parameter
func cellAddItemAction(sender: UIButton){
var count = (String(self.itemQty.text!)).toInt()!
totalPrice = (self.itemPriceLabel!.text! as NSString).doubleValue
count += 1
NSNotificationCenter.defaultCenter().postNotificationName(priceUpdateKey, object: nil, userInfo: ["totalPrice":totalPrice])
}
Then back in the viewController, add an observer for the NSNotification:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "updatePrice:", name: priceUpdateKey, object: nil)
And finally write the function that the observer calls:
func updatePrice(sender: NSNotification) {
let userInfo:Dictionary<String,Double!> = sender.userInfo as Dictionary<String,Double!>
let messagePrice = userInfo["totalPrice"]
let finalCalc = totalAmount + messagePrice!
totalAmount = finalCalc
totalPriceButton.setTitle("£\(totalAmount)0", forState: UIControlState.Normal)
}'
Note** I'm not claiming this is a good method, but it works for me, and if it helps you, perfect.
Upvotes: 1