使用 TableView 从 ControlView 获取字符串

Posted

技术标签:

【中文标题】使用 TableView 从 ControlView 获取字符串【英文标题】:Getting a String from a ControlView with TableView 【发布时间】:2016-09-08 05:08:17 【问题描述】:

我成功设置了我的表格视图,以便它可以根据被点击的行正确传递特定的字符串。但是,我不知道如何检索这些数据。我知道如何在 java 中做到这一点,但我是 swift 新手,我觉得它很混乱。

发送方控制视图:

import UIKit

class TechniqueListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource 

    let cellContent = ["Stance", "Move Forward", "Move Backward", "Move Right", "Move Left", "Jab", "Cross", "Hook", "Uppercut", "Body Jab", "Body Cross", "Body Hook", "Body Uppercut", "Leg Kick", "Body Kick", "Switching Stances", "Switch Leg Kick", "Switch Body Kick", "Push Kick", "Switch Push Kick", "Front Push Kick", "Switch Front Push Kick", "Spinning Back Kick", "Knee", "Switch Knee", "Elbow", "Tornado Kick"]

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
        //gets # of row
        return cellContent.count
    

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
        //defines content of each cell

        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "TechniqueCell")
        cell.textLabel?.text = cellContent[indexPath.row]

        return cell

    

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
        let cellIndex = indexPath.row
        if (cellIndex == 0)
            performSegue(withIdentifier: value(forKey: "Stance") as! String, sender: IndividualTechniqueController())
        
        else if cellIndex == 1
            performSegue(withIdentifier: "Move Forward", sender: IndividualTechniqueController())
        
    


    override func viewDidLoad() 
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    

检索 ControlView:

    //I want an if else statement here
    if senderString == "Stance"  //  <---- correct me if this is wrong
    
    else if senderString == "Move Forward"
    


【问题讨论】:

哪些是 cellContent 数组中的字符串,也是您在 didSelectedRowAt indexPath 方法中究竟想做什么? 什么是IndividualTechniqueController 是当前控制器吗?你也只是想从cellContent 数组传递字符串对象?还显示故事板 segue 及其您创建的标识符。 在索引 0-1: cellContent = ["Stance", "Move Forward"] 我只想将这些字符串作为 switch 语句放在另一个控件视图中,这样我就知道要使用哪些 UI 组件对于第二个控制视图 我将编辑我的问题 @GrantEspanet 你的destinationController 对于两个单元格都一样吗? 【参考方案1】:

您好,在 TechniqueListViewController 中取一个数据类型为 String(您需要的数据类型)的变量,如下所示

var previouspageData: String

在 tableview didselect 方法中使用该变量将数据发送到该控制器

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 

self.previouspageData = "your data"
 let cellIndex = indexPath.row
if (cellIndex == 0)
    performSegue(withIdentifier: value(forKey: "Stance") as! String, sender:self)

else if cellIndex == 1
     performSegue(withIdentifier: "Move Forward", sender:self)


在您的目标控制器中使用该数据,如下所示

 override func performSegueWithIdentifier(identifier: String, sender: AnyObject?) 
if sender is TechniqueListViewController 
 if  (sender as! TechniqueListViewController).previouspageData == "Stance" 
 
 else if (sender as! TechniqueListViewController).previouspageData == "Move Forward"
 

  


【讨论】:

以上是关于使用 TableView 从 ControlView 获取字符串的主要内容,如果未能解决你的问题,请参考以下文章

为啥我在使用 kingfisher 将图像从 JSON 加载到 tableview 时出错?

如何使用 Swift 保存从 tableView 上的 textField 收到的用户答案

将数据从 Tableview 传递到 Tableview 详细信息

如何从屏幕底部弹出 tableView?

从 TableView 中获取数据(CoreData)

从 Firebase 中基本读取并使用结果加载 tableview (Swift)