-1

I tried this but not worked Iterate over all the UITableCells given a section id I need to just iterate a for loop in cell and calculate the value and display in another label calculated_value or print I dont know how to do it. Any help will be great! I got only value for cell I see the cell which are at bottom there value is not calculated. when I scroll up I get the cell value but then the top cell value is not able to see or calculate.

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
  let news = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return news.count
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = myTableView.dequeueReusableCell(withIdentifier: "mycellTVC") as! mycellTVC
    cell.lbl.text = news[indexPath.row]

    return cell
  }


  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 80
  }

  @IBOutlet weak var myTableView: UITableView!

  override func viewDidLoad() {
    super.viewDidLoad()
    myTableView.delegate = self
    myTableView.dataSource = self

  } 
}
 **This is my cell file**
class mycellTVC: UITableViewCell{

  @IBOutlet weak var lbl: UILabel!
  @IBOutlet weak var calculated_value: UILabel!
}
rmaddy
  • 307,833
  • 40
  • 508
  • 550
arman khan
  • 23
  • 6
  • I don't see any calculation in your code. What is that you want to achieve ? – Nitish Apr 26 '18 at 12:54
  • are you trying to achieve the addition of news.? – Abhirajsinh Thakore Apr 26 '18 at 12:55
  • Please check I have editied and it creates issue when I the cell is below and I am not able to get it's value not calculate – arman khan Apr 26 '18 at 12:55
  • Could you clarify what are you aiming to do? the table view has only one section containing `news.count` as number of rows and your code should work fine so far... – Ahmad F Apr 26 '18 at 12:56
  • To be more clear I want to get the addition of all value in label for all 10 rows print or another label by using for loop. – arman khan Apr 26 '18 at 13:03
  • tinyurl.com/yd9cracg – arman khan Apr 26 '18 at 13:13
  • dont try calculations based on UI element value in a cell. use the data source to do it – Scriptable Apr 26 '18 at 13:26
  • @Scriptable I don't know how . Please share something to get it worked – arman khan Apr 26 '18 at 13:29
  • you need to clarify exactly what you want. You want total of numbers in news array? so in this case result would be 55? – Scriptable Apr 26 '18 at 13:44
  • @Scriptable I have 10 rows and I have one label in cell on which I pass value from api and I want calculate that value and display in another label is my goal . same as we do in shopping app on order screen and please copy and paste check this link tinyurl.com/yd9cracg you will get to know what I am exactly trying to say – arman khan Apr 26 '18 at 13:48

1 Answers1

0

The basics of what you are trying to do are to get a value from the data source, convert it to a number if needed and then total them up.

For your example above, this would do the trick:

let news = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]

let total = news.map({ Int($0)! }).reduce(0) { x, y in
    return x + y
}

print(total) // 55

To do this for a shopping cart, your data source will likely be a list of products instead, maybe something like:

struct Product
{
    var name: String
    var price: Int
    var quantity: Int
}

let prod1 = Product(name: "Coka Cola", price: 2, quantity: 2)
let prod2 = Product(name: "Bread", price: 1, quantity: 1)
let prod3 = Product(name: "Sweets", price: 2, quantity: 4)

let shoppingCart = [prod1, prod2, prod3]

let total = shoppingCart.reduce(0) { x, y in
    return x + (y.price * y.quantity)
}

print(total) // 13

Its the same principal, just a little more complicated. As long as your data structure is right it should be straight forward

Scriptable
  • 18,734
  • 5
  • 56
  • 68
  • it worked and thank you. I have a confusion for example If i have 3 text field like name A, B, C in each row and 10 rows . I am performing some calculation A*B result is stored in C . I am passing value to B from API but after editing the B when I scroll up and down it gives same value which I got from API. How can I append the live edited value and pass it to text Field – arman khan Apr 26 '18 at 14:17
  • when you change any values you should be updating the underlying data. Cells in a collection/tableview can be reused when not in view. so if you have large cells for example you might only fit 5 on screen. you might only have 5 loaded at all, maybe 6. so if you update values in a cell and then its unloaded and reused for another one. then you lose your data. so always rely on DATA not the UI – Scriptable Apr 26 '18 at 14:27
  • thank you . I tried with data I run a for loop in table view cell to get all data my table view screen fits only 2 cell at a time and other cell are below. So I get data for first 2 cell and for other cell below I get "NIL" I tried many solutions but not worked – arman khan Apr 27 '18 at 05:30
  • **you cannot get data from invisible cells** (you could.. but you really, really shouldn't) use the underlying data. you create a cell on demand from data, if you update the data in a cell, update the data too. always do calculations on the data, not the cell itself – Scriptable Apr 27 '18 at 06:14
  • Check this answer to see how to do update the data after editing: https://stackoverflow.com/questions/32669759/update-array-while-editing-cell-while-running-the-app – Scriptable Apr 27 '18 at 06:25
  • That was great I tried and it worked. I get the value proper and now it doesn't reload the older value I get new values. – arman khan Apr 27 '18 at 11:33