如何将单元格标签的值从 tableView 传递到其他 View?
How to pass cell label's value from tableView to other View?
我是 IOS 编程的初学者,也是整个编程的初学者。
(我有XCODE 6.4)
看了那么多教程,还是没有找到自己需要的资料
我有一个代码可以为标签赋值:
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
}
然后,我有一个代码,它必须获取标签的名称(我认为是)
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)
}
最后是代码,它将数据从标签传递到另一个 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
}
}
它必须这样工作:
Table 视图获取单元格的数据(这里没有代码)
然后,调用 TablView 的方法必须获取单元格的标签。
最后,我单击该单元格,然后移动到另一个 ViewController,我的单元格标签数据打印在另一个标签中。
但它不起作用,所以当我单击单元格时,我移动到 ViewController 并且 Label 中的文本等于 nil(我看不到任何文本)。为什么会这样?帮我解决这个问题!
感谢您的所有建议!
您的问题是您正在使用函数dequeueReusableCellWithIdentifier
来获取单元格,并且此方法仅 returns 一个单元格,如果它已被标记为可以重用。
您需要使用与委托方法不同的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)
}
希望对你有所帮助。
我是 IOS 编程的初学者,也是整个编程的初学者。
(我有XCODE 6.4)
看了那么多教程,还是没有找到自己需要的资料
我有一个代码可以为标签赋值:
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
}
然后,我有一个代码,它必须获取标签的名称(我认为是)
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)
}
最后是代码,它将数据从标签传递到另一个 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
}
}
它必须这样工作: Table 视图获取单元格的数据(这里没有代码) 然后,调用 TablView 的方法必须获取单元格的标签。 最后,我单击该单元格,然后移动到另一个 ViewController,我的单元格标签数据打印在另一个标签中。
但它不起作用,所以当我单击单元格时,我移动到 ViewController 并且 Label 中的文本等于 nil(我看不到任何文本)。为什么会这样?帮我解决这个问题!
感谢您的所有建议!
您的问题是您正在使用函数dequeueReusableCellWithIdentifier
来获取单元格,并且此方法仅 returns 一个单元格,如果它已被标记为可以重用。
您需要使用与委托方法不同的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)
}
希望对你有所帮助。