简体   繁体   English

如何将单元格标签的值从tableView传递到其他View?

[英]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.

相关问题 是否需要将tableView的值传递给另一个视图的标签? - Need to pass a value from my tableView to a label on another view? 如何将数据从Firebase在tableview单元之间传递到视图控制器? - how to pass data from firebase between tableview cell to view controllers? 如何将 TableView 的回调传递给 TableView Cell? - How to pass callback from TableView to TableView Cell? 将信息从tableview传递给视图控制器而不是基于indexPathForSelectedRow,而是传递给选定单元格的标签文本? - Pass information from a tableview to a view controller based not on indexPathForSelectedRow, but on label text of cell selected? 如何从TableView到另一个视图获取单元格的值 - How to get the Value of a Cell from a TableView to Another View 将TableView单元的Firebase数据检索到视图控制器的标签 - Retrieving firebase data of a tableview cell to a view controller's label 如何将哪个tableview单元格作为导航标题传递给其他视图控制器? - How to pass what tableview cell was pressed to other view controllers as navigation title? 如何通过点击表格视图单元格内的标签来显示另一个视图控制器 - How to present another view controller by tapping on a label inside a tableview cell 如何将值从表视图单元格传递到视图控制器 - how to pass the value from table view cell to view controller 如何将数据从tableview传递到另一个tableview单元? - how to pass data from tableview to another tableview cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM