[英]How to pass cell label's value from tableView to other View?
I am a beginner in IOS programming, and in a whole programming. 我是IOS编程以及整个编程的初学者。
(I have XCODE 6.4) (我有XCODE 6.4)
I have read so many tutorials, but i haven't found the information I need. 我已经阅读了很多教程,但是没有找到所需的信息。
I have a code which assign a value to a label : 我有一个为标签分配值的代码:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identifier = "formuleTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! formule
let formuleCommand = formulesList[indexPath.row]
// Configure the cell...
var shortCut = formuleCommand.formuleText
cell.formuleLabel.text = shortCut
return cell
}
And then, I have a code, which have to get the label's name (I think so) 然后,我有一个代码,必须获取标签的名称(我认为是这样)
var valueToPass:String!
func tablView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("You selected cell #\(indexPath.row)!")
// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow();
let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;
let identifier = "formuleTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath!) as! formule
valueToPass = cell.formuleLabel.text
performSegueWithIdentifier("detail", sender: self)
}
And finally, code, which passes the data from label to another ViewController 最后是代码,它将数据从标签传递到另一个ViewController
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "detail") {
// initialize new view controller and cast it as your view controller
var viewController = segue.destinationViewController as! specialitiesViewController
// your new view controller should have property that will store passed value
viewController.passedValue = valueToPass
}
}
It have to work so: Table view gets the data for cells (here is no code for this) Then, method called TablView have to get cell's label. 它必须这样工作:表视图获取单元格的数据(此处没有代码)然后,称为TablView的方法必须获取单元格的标签。 And finally, i click on the cell and I move to another ViewController, where my Cell,s Label data prints in another Label.
最后,我单击该单元格,然后移到另一个ViewController,在这里,我的Cell,s Label数据打印在另一个Label中。
But it don't work so, when I click on cell, I move to ViewController and the text in Label equals nil (i see no text). 但这是行不通的,当我单击单元格时,我移至ViewController,Label中的文本等于nil(我看不到任何文本)。 Why does it work so?
为什么这样工作? Help me to fix this issue!
帮我解决这个问题!
Thank you, for all your suggestions! 谢谢您的所有建议!
Your problem is that you're using the function dequeueReusableCellWithIdentifier
for get the cell and and this method only returns a cell if it has been marked as ready for reuse. 您的问题是您正在使用函数
dequeueReusableCellWithIdentifier
来获取单元格,并且此方法仅在已将其标记为准备好重用的情况下才返回单元格。
You need to use cellForRowAtIndexPath
that is different from the delegate method, be carefull to get the cell, change your didSelectRowAtIndexPath
like the following: 您需要使用不同于委托方法的
cellForRowAtIndexPath
,请谨慎获取单元格,如下更改您的didSelectRowAtIndexPath
:
func tablView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("You selected cell #\(indexPath.row)!")
// get cell label
let cell = tableView.cellForRowAtIndexPath(indexPath) as! formule
self.valueToPass = cell.formuleLabel.text
self.performSegueWithIdentifier("detail", sender: self)
}
I hope this help you. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.