I have searched the internet countless times and have not found a solution to my situation. Things that may be a solution are things I didn\'t understand and they were in Ob
First retrieve your cell where you need it (for instance in didSelectRowAtIndexPath). Cast your UITableViewCell in your custom cell. And then access properties of your cell as you wish.
Since you haven't provided any code, I will provide simply examples of the code:
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 submit button, you want to submit the data right? And you don't have indexPath there...
Well you have several options, one is to go through each cell, check the type and get it. But seems like your each cell is different one. And seems like you have 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 entire cell to get one value? How about you create few variables (or even better, array) and extract values there? you can link events to these controls and when they change you get these value and save them. Later on, you can use these values(or array) without accessing cells or retrieving them.
EDIT:
How about tags? I am not sure if you are adding this through 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 that question is your custom class). That way, you can go through questions and take specific ones.
Here is 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
}
I know this is a very old post but then to if it helps someone I'll be happy so to do this the simple way which I will prefer is made an array of the UI table view cell and called it in the UI View Controller and then this becomes very simple just called the array (Thanks to rohan)
(FOR EXAMPLE)
import UIKit
public class PlanningTableView:UITableViewCell{
@IBOutlet weak var Sccode: UITextField!
@IBOutlet weak var NoOfPatients: UITextField!
}
(AND NOW IN VIEW CONTROLLER DO THIS)
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 at sumbmit Button just do this /////////////////
@IBAction func Submit(_ sender: Any) {
CollectionOfCell.forEach { cell in
ScCode.append(cell.Sccode.text!)
NoOfPatient.append(cell.NoOfPatients.text!)
}
}
For more details I just created a YouTube first ever tutorial - comments good/bad appreciated
To collect data from your cells after the user has finished entering the data (score and level), you can use the UITableView
delegate method tableView:didDeselectRowAtIndexPath
[Deselect]
The code in your instance will go in 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 you submit button's action(Selector) function use the 'levelArray' and 'scoreArray' to pass your collected data. Be sure to declare the array/dictionary variables(ex: levelArray) right under your UITableViewController
class declaration, like so
class GpaCalculator: UITableViewController {
var levelArray:[String] = []
var scoreArray:[String] = []
override func viewDidLoad() {
super.viewDidLoad()
}
// ...
}
..to be able to use it in other functions like your submit button's action(Selector) function.