Ios 如何将UITableViewCell值正确转换为字符串
因此,我的代码如下所示:Ios 如何将UITableViewCell值正确转换为字符串,ios,swift,uitableview,Ios,Swift,Uitableview,因此,我的代码如下所示: override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){ let selectedCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell performSegueWithIdentifier("TVtoVCSegue", sender: sele
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let selectedCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell
performSegueWithIdentifier("TVtoVCSegue", sender: selectedCell)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "TVtoVCSegue" {
let otherViewController = segue.destinationViewController as! TTDetailCntr
otherViewController.CurrentClass = String(sender!) as String
}
cell.textLabel?.text
override func tableView(
tableView: UITableView,
didSelectRowAtIndexPath indexPath: NSIndexPath)
{
//You would need to create the instance var cellIndex
self.cellIndex = indexPath.row
let selectedCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell
performSegueWithIdentifier("TVtoVCSegue", sender: selectedCell)
}
override func prepareForSegue(segue: UIStoryboardSegue,
sender: AnyObject?)
{
if segue.identifier == "TVtoVCSegue"
{
//This code assumes that myModel is an array of structs.
//Change this code to fit your data model
//(should be the same code you use to set the text in the cell.)
let stringToPass = myModel[self.cellIndex].stringValue
let otherViewController = segue.destinationViewController
as! TTDetailCntr
otherViewController.CurrentClass = stringToPass
}
}
otherViewController.CurrentClass
连接到另一个viewController的标签,其标签上显示:
<UITableViewCell: 0x13f692230; frame = (0 132; 320 44); text = '10.d'; clipsToBounds = YES; autoresize = W; layer = <CALayer: 0x13f692600>>
在这种情况下,它应该是“10.d”。我怎样才能做到这一点
我查了很多问题,如果你发现了重复的问题,请告诉我或解释我做错了什么
提前感谢您可以访问UITableViewCell“cell”的文本标签,如下所示:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let selectedCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell
performSegueWithIdentifier("TVtoVCSegue", sender: selectedCell)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "TVtoVCSegue" {
let otherViewController = segue.destinationViewController as! TTDetailCntr
otherViewController.CurrentClass = String(sender!) as String
}
cell.textLabel?.text
override func tableView(
tableView: UITableView,
didSelectRowAtIndexPath indexPath: NSIndexPath)
{
//You would need to create the instance var cellIndex
self.cellIndex = indexPath.row
let selectedCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell
performSegueWithIdentifier("TVtoVCSegue", sender: selectedCell)
}
override func prepareForSegue(segue: UIStoryboardSegue,
sender: AnyObject?)
{
if segue.identifier == "TVtoVCSegue"
{
//This code assumes that myModel is an array of structs.
//Change this code to fit your data model
//(should be the same code you use to set the text in the cell.)
let stringToPass = myModel[self.cellIndex].stringValue
let otherViewController = segue.destinationViewController
as! TTDetailCntr
otherViewController.CurrentClass = stringToPass
}
}
因此,您希望在prepareforsgue函数中进行以下更改
if let cell = sender as? UITableViewCell, text = cell.textLabel?.text {
otherViewController.CurrentClass = text
}
大概您在表视图的某个位置安装了一个数组中的文本。您应该将所选单元格的索引记录到didSelectRowAtIndexPath中的实例变量中,然后使用该变量在prepareForSegue方法中获取表中该项的字符串 大概是这样的:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let selectedCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell
performSegueWithIdentifier("TVtoVCSegue", sender: selectedCell)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "TVtoVCSegue" {
let otherViewController = segue.destinationViewController as! TTDetailCntr
otherViewController.CurrentClass = String(sender!) as String
}
cell.textLabel?.text
override func tableView(
tableView: UITableView,
didSelectRowAtIndexPath indexPath: NSIndexPath)
{
//You would need to create the instance var cellIndex
self.cellIndex = indexPath.row
let selectedCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell
performSegueWithIdentifier("TVtoVCSegue", sender: selectedCell)
}
override func prepareForSegue(segue: UIStoryboardSegue,
sender: AnyObject?)
{
if segue.identifier == "TVtoVCSegue"
{
//This code assumes that myModel is an array of structs.
//Change this code to fit your data model
//(should be the same code you use to set the text in the cell.)
let stringToPass = myModel[self.cellIndex].stringValue
let otherViewController = segue.destinationViewController
as! TTDetailCntr
otherViewController.CurrentClass = stringToPass
}
}
虽然将模型数据存储在表视图单元格中是一种糟糕的设计。最好从模型对象获取数据并传递出去。@Duncac我非常同意,但我认为这可能超出了他的问题范围。@Duncac我知道,但一旦我的项目开始工作,我就会开始改进代码设计。“一旦我的项目开始工作,我就会开始改进代码设计。”这不是设计,这是一个意大利面条代码的配方。@KarlKivi如果这回答了您的问题,您不需要任何帮助,请不要忘记将其作为答案接受。好的,这段代码正常,但viewController加载了两次?我读到我不能同时实现didselectrowatinexpath和prepareforsgue函数,我必须只实现prepareforsgue并将所有代码放在那里。我应该再问一个问题还是你能帮我@邓肯