Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
  2. {
  3.  
  4. let cell = ingredientTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! IngredientTableViewCell
  5.  
  6. cell.ingredientNameTextField.text = ingredients [indexPath.row].ingredientName
  7. cell.numberofItem.text = "1"
  8. let cellcalory = ingredients [indexPath.row].ingredientCalory
  9. cell.itemTotalCalory.text = cellcalory
  10.  
  11. cell.plusButton.tag = Int(cell.itemTotalCalory.text!)! //indexPath.row
  12. cell.plusButton.addTarget(self, action:#selector(plusAction), for: .touchUpInside)
  13. cell.minusButton.tag = Int(cell.itemTotalCalory.text!)!
  14. cell.minusButton.addTarget(self, action:#selector(minusAction), for: .touchUpInside)
  15.  
  16. return cell
  17. }
  18.  
  19.  
  20. @IBAction func plusAction(sender: UIButton)
  21. {
  22.  
  23. let cell = ingredientTableView.dequeueReusableCell(withIdentifier: "cell") as! IngredientTableViewCell
  24. let buttonRow = sender.tag
  25.  
  26. if cell.numberofItem.text == "1" || cell.numberofItem.text != "1"
  27. {
  28. cell.numberofItem.text = "1"
  29. let textValue1 = cell.numberofItem.text
  30. var textValue = Int(textValue1!)
  31. textValue = textValue! + 1
  32. cell.numberofItem.text = String(describing: textValue)
  33.  
  34. let oldcalory = buttonRow
  35. cell.itemTotalCalory.text = String (((textValue! * Int(oldcalory)) + Int(oldcalory)))
  36. let newcalory = cell.itemTotalCalory.text
  37.  
  38. refresh(newcalory: newcalory!);
  39.  
  40. }
  41. }
  42.  
  43. func refresh(newcalory :String)
  44. {
  45.  
  46. let cell = ingredientTableView.dequeueReusableCell(withIdentifier: "cell") as! IngredientTableViewCell
  47. cell.itemTotalCalory.text = newcalory
  48.  
  49. DispatchQueue.main.async {
  50. self.ingredientTableView.reloadData()
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement