Getting Data from Each Uitableview Cells Swift

getting data from each UITableView Cells Swift

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
}

How to get data from UITableView cell to a global variable

Try to cast the sender.view as a UITableViewCell, then you should be able to access the cell's textLabel.

guard let cell = sender.view as? UITableViewCell else {
//error handling
return
}

let text = cell.textLabel.text

Get uitextfield data from custom uitableview cell - Swift

First issue -- in your cell class, in awakeFromNib() you have:

// CREATE a UITextField
var textField = UITextField()
var data: Dictionary<String, Any>?

override func awakeFromNib() {
super.awakeFromNib()

// assign self as the delegate of textField
textField.delegate = self

let padding = UIView(frame: CGRect(x:0, y:0, width: 15, height: self.frame.height))

// CREATE a NEW UITextField
textField = UITextField(frame: CGRect(x:0, y:0, width: self.frame.width, height: 40))

So, when you create the NEW textField, it no longer has a delegate assigned to it. That's why nothing triggers textFieldShouldReturn or textFieldDidEndEditing.

Replace the UITextField(frame: ..) line with:

    textField.frame = CGRect(x:0, y:0, width: self.frame.width, height: 40)

Next, instead of passing in a "data object" to then try and manipulate, think about using a call-back closure.

Change your cell class to this:

class RTextCell: UITableViewCell, UITextFieldDelegate {

var textField = UITextField()

// this will be our "call back" closure
var didEndEditAction : ((String)->())?

override func awakeFromNib() {
super.awakeFromNib()

textField.delegate = self

let padding = UIView(frame: CGRect(x:0, y:0, width: 15, height: self.frame.height))

// just set the frame, don't create a NEW textField
textField.frame = CGRect(x:0, y:0, width: self.frame.width, height: 40)
textField.placeholder = "Your text here"
textField.textColor = UIColor.white
textField.tintColor = UIColor.white
textField.backgroundColor = UIColor.clear
textField.leftView = padding
textField.leftViewMode = .always

self.addSubview(textField)

}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
print("Should return")
return true
}

func textFieldDidEndEditing(_ textField: UITextField) {
// call back using a closure
didEndEditAction?(textField.text ?? "")
}

}

And then configure it like this:

    case "textbox":
let rTemplateTextBox = tableView.dequeueReusableCell(withIdentifier: "recapTextCell", for: indexPath) as! RTextCell
rTemplateTextBox.textField.text = cell["data"] as? String ?? "No text"

// setup the "call back" closure
rTemplateTextBox.didEndEditAction = {
(newText) in
let cell = self.myData[indexPath.row]
cell["data"] = newText
}

return rTemplateTextBox

Swift TableView Cell Unable to Display the data from API

You are registering a generic cell:

self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "DisplayView1")

You need to register your custom cell:

self.tableView.register(DisplayView.self, forCellReuseIdentifier: DisplayView.identifier)

How to get the label values from multiple selected cells in a UITableView and pass them to a different ViewController swift

Override the prepare(for segue: UIStoryboardSegue, sender: Any?) in the AddContactsListTableView class where you can pass the selected contacts to the next view controller.

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Pass the selected object to the new view controller.
if let selectedRows = tableView.indexPathsForSelectedRows {
let selectedContacts = selectedRows.map{contacts[$0.row]}
let newVC = segue.destination as! NewViewController
newVC.contacts = selectedContacts
}
}

See this tutorial for more.



Related Topics



Leave a reply



Submit