Reputation: 195
I have a UITableViewController that has a function named didselectrowatindex that should return the text on a cell, but it returns nil. How can I return the text of the selected cell in Swift UITableViewController?
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indexPath = tableView.indexPathForSelectedRow();
let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;
println(currentCell.textLabel!.text)
}
}
EDIT: I forgot to mention that I am using static cells, which means the data is already set so I didn't think there was a need for cellforrowatindex.
Upvotes: 1
Views: 637
Reputation: 1789
It looks like you need to implement cellForRowAtIndexPath
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell")
let text = self.tableContents[indexPath.row] as! String
//tableContents is just the array from which you're getting what's going in your tableView, and should be declared outside of your methods
cell.textLabel?.text = text
return cell
}
also add
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
into you viewDidLoad hope this helps!
Okay, so looking at it that way, maybe try to replace the line
let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;
with
let currentCell = super.tableView(self.tableView, cellForRowAtIndexPath: indexPath)
and get rid of the line above it as suggested by Lancelot in a comment on your question.
I used the following code and it works:
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let currentCell = super.tableView(self.tableView, cellForRowAtIndexPath: indexPath)
let text: String = currentCell.textLabel!.text as String!
print(text)
}
}
Upvotes: 1
Reputation: 1442
Get selected row cell from tableView(sender) and then get your data from selected cell.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var currentCell= tableView.cellForRowAtIndexPath(indexPath)! as tableViewCell
println(currentCell.textLabel!.text)
}
Upvotes: 0
Reputation: 5326
If you have your data source array of the titles as:
var arrTitlesDataSource = ["t1","t2","t3"]
So you are using them in order to set the title at the cellForRow
like this:
cell.textLabel?.text = arrTitlesDataSource[indexPath.row]
Than you may get the title in this way:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var title = arrTitlesDataSource[indexPath.row]
println(title)
}
Upvotes: 0