Retrieving data from each UITableView Swift cell

I searched the Internet many times and did not find a solution for my situation. Things that might be the solution is something that I did not understand, and they were in Objective-C. So if it is a duplicate, it is not. I could not find a solution from other posts.

I make a GPA calculator specifically for my school, where we get different points depending on our levels.

I made a UITableView with a custom cell that will be duplicated a certain number of times for each item in the class.

What I want to know is getting data from each of these user cells (grade and level)

So this is my storyboard:

Storyboard

and this is my application viewed in the simulator:

App screenshots

I am going to get a grade and level by getting the text of the labels for each topic, and I have no idea how to get data from certain cells.

Many thanks.

Here is the code that I have:

//showStepperValueLabel shows the level and showSliderValueLabel shows the score in the cells. //customCell is my custom class for my custom cell. //i have already declared the levels and scores array above in my class func tableView(tableView: UITableView!, didDeselectRowAtIndexPath indexPath: NSIndexPath!) { let cell = tableView.cellForRowAtIndexPath(indexPath) as customCell var level: String! = cell.showStepperValueLabel.text var score: String! = cell.showSliderValueLabel.text levels[indexPath.row] = level scores[indexPath.row] = score } //yadiyadayada //and this is the part where the values get received(it inside the prepareForSegue function) var engScore: String = scores[0] var engLevel: String = levels[0] var mathScore: String = scores[1] var mathLevel: String = levels[1] var sciScore: String = scores[2] var sciLevel: String = levels[2] var geoScore: String = scores[3] var geoLevel: String = levels[3] var hisScore: String = scores[4] var hisLevel: String = levels[4] var chiScore: String = scores[5] var chiLevel: String = levels[5] // 

But I get an error when arrays never received values. can someone help?

EDIT:

I got an error again, so I tried to manually pass strings to arrays, and its initialization, for example

 var levels: [String] = ["H", "H", "H", "H", "H", "H"] var scores: [String] = ["12", "23", "34", "45", "56", "67"] 

and the program worked perfectly. Thus, it is concluded that the problem arises in the part where the array receives strings that

 func tableView(tableView: UITableView!, didDeselectRowAtIndexPath indexPath: NSIndexPath!) { let cell = tableView.cellForRowAtIndexPath(indexPath) as customCell var level: String! = cell.showStepperValueLabel.text var score: String! = cell.showSliderValueLabel.text levels.insert(level, atIndex: indexPath.row) scores.insert(level, atIndex: indexPath.row) } 

Are you sure the selection is correct? everyone else on the internet taught me how to use tags, but didn't tell me how to use it ...

EDIT2:

so I tried to use tags. this is what i wrote in the tableView function: cellForRowAtIndexPath

 cell.showStepperValueLabel.tag = indexPath.row+10 cell.showSliderValueLabel.tag = indexPath.row 

and this is what i wrote in prepareForSegue file

 var engScore : UILabel! = self.view.viewWithTag(0) as? UILabel var mathScore: UILabel! = self.view.viewWithTag(1) as? UILabel var sciScore: UILabel! = self.view.viewWithTag(2) as? UILabel var geoScore: UILabel! = self.view.viewWithTag(3) as? UILabel var hisScore: UILabel! = self.view.viewWithTag(4) as? UILabel var chiScore: UILabel! = self.view.viewWithTag(5) as? UILabel var engLevel: UILabel! = self.view.viewWithTag(10) as? UILabel var mathLevel: UILabel! = self.view.viewWithTag(11) as? UILabel var sciLevel: UILabel! = self.view.viewWithTag(12) as? UILabel var geoLevel: UILabel! = self.view.viewWithTag(13) as? UILabel var hisLevel: UILabel! = self.view.viewWithTag(14) as? UILabel var chiLevel: UILabel! = self.view.viewWithTag(15) as? UILabel 

so in the GPA calculation function I set

 //Get pxcs engpxc = engCredits*co.getEnglishPoints(engLevel.text!, engScore: engScore.text!) mathpxc = mathCredits*co.getNonLanguagePoints(mathLevel.text!, scoreRecieved: mathScore.text!) geopxc = geoCredits*co.getNonLanguagePoints(geoLevel.text!, scoreRecieved: geoScore.text!) hispxc = hisCredits*co.getNonLanguagePoints(hisLevel.text!, scoreRecieved: hisScore.text!) scipxc = sciCredits*co.getNonLanguagePoints(sciLevel.text!, scoreRecieved: sciScore.text!) chipxc = chiCredits*co.getChiPoints(chiLevel.text!, chiScore: chiScore.text!) // 

and now I get an error

Fatal error: Zero unexpectedly found while deploying optional value (Lldb)

can someone help me with this?

EDIT3 - Additional Information:

I added println to tableView: cellForRowAtIndexPath in the parts where I give the tags, and found out that the tags were sent successfully, and these tags got the tags that I assigned in the program, however, when I checked with println in the prepareForSegue function in where variables get their views to see if they got shortcuts, but I got "zero". What is the problem?

+5
source share
3 answers

First, extract your cell where you need it (for example, in the didSelectRowAtIndexPath file). Add your UITableViewCell to your custom cell. And then the access properties of your cell, as you wish.

Since you did not specify any code, I will give just code examples:

 func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { let cell = tableView.cellForRowAtIndexPath(indexPath) as YourCell //cell.value, cell.text, cell.randomValue or whatever you need } 

What about the submit button, do you want to transfer the data correctly? And you do not have indexPath ...

You have several options: you need to go through each cell, check the type and get it. But it looks like each cell is different. And it looks like you have an order for these cells. So you know where exactly your result will be. So in submit you can do the following

 @IBAction func submit_pressed(sender: UIButton) { var indexs = NSIndexPath.init(index: 10) // or which one(s) you need. you extract data from the cell similar to previous function } 

But why do you have to get the whole cell to get one value? How about you create multiple variables (or even better, an array) and retrieve the values โ€‹โ€‹there? You can associate events with these controls, and when they change, you get these values โ€‹โ€‹and save them. Later you can use these values โ€‹โ€‹(or an array) without accessing the cells or getting them.

EDIT:

What about tags? I'm not sure if you are adding this via code or storyboard, but I will go through both of them.

In cellForRowAtIndexPath you can simply say cell.tag = indexPath.row or even better: cell.tag = question.id (assuming this question is your regular class). This way you can go through the questions and take concrete ones.

Here is the working code with tags:

  @IBAction func test(sender: AnyObject) { var lbl : UILabel = self.tableView.viewWithTag(12) as UILabel! var cell : UITableViewCell = self.view.viewWithTag(3) as UITableViewCell! return ; } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = indexPath.row == 0 ? tableView.dequeueReusableCellWithIdentifier("firstCell", forIndexPath: indexPath) as UITableViewCell : tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell if(indexPath.row != 0){ let item = toDoList[indexPath.row-1] cell.textLabel?.text = item.title NSLog("%d",indexPath.row) var integerncina = (indexPath.row + 10) as NSInteger! cell.textLabel?.tag = integerncina NSLog("%d", cell.textLabel!.tag) cell.tag = indexPath.row } // Configure the cell... return cell } 
+9
source

To collect data from your cells after the user has completed entering data (grade and level), you can use the delegate method of the UITableView tableView:didDeselectRowAtIndexPath [ Deselect ]

The code in your instance will pass in the UITableViewController class and will look something like this.

 override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { let cell = tableView.cellForRowAtIndexPath(indexPath) as YourCustomCellClass var levelArray:[String] = [] //Assuming you want to collect and store your cell data in an Array. You may use a Dictionary as well, whichever is more convenient. var scoreArray:[String] = [] // levelArray.append(cell.levelLabel.text!) // scoreArray.append(cell.scoreLabel.text!) levelArray.insert(cell.levelLabel.text!, atIndex: indexPath.row) scoreArray.insert(cell.scoreLabel.text!, atIndex: indexPath.row) } 

Next, in your Selector function, use "levelArray" and "scoreArray" to transfer your collected data. Be sure to declare array / dictionary variables (e.g .: levelArray) right below the declaration of the UITableViewController class, e.g.

 class GpaCalculator: UITableViewController { var levelArray:[String] = [] var scoreArray:[String] = [] override func viewDidLoad() { super.viewDidLoad() } // ... } 

.. to use it in other functions, such as the action function of the submit button (Selector).

+3
source

I know this is a very old post, but then, if it helps someone, I will be happy, therefore, for this, in an easy way that I prefer, an array of the presentation cell of the user interface table is made and called in the View Controller user interface, and then it becomes very simple, just called an array (thanks rohan)

(FOR EXAMPLE)

 import UIKit public class PlanningTableView:UITableViewCell{ @IBOutlet weak var Sccode: UITextField! @IBOutlet weak var NoOfPatients: UITextField! } 

(AND NOW IN THE VIEWED CONTROLLER DO IT)

 import UIKit class PlanningReport:UIViewController,UITableViewDelegate,UITableViewDataSource{ var ScCode = [String]() var NoOfPatient = [String]() var CollectionOfCell = [PlanningTableView]() func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PlanningTableView CollectionOfCell.append(cell) return cell } 

//////// Then on sumbmit Button just do it ///////////////

 @IBAction func Submit(_ sender: Any) { CollectionOfCell.forEach { cell in ScCode.append(cell.Sccode.text!) NoOfPatient.append(cell.NoOfPatients.text!) } } 

For more information, I just created the first YouTube tutorial - comments good / bad rated

0
source

Source: https://habr.com/ru/post/1213046/


All Articles