向TableView行添加信息/详细信息

时间:2019-02-09 17:55:11

标签: swift segue viewcontroller

我有一个TableView,第一个ViewController上有项目。当我单击一行时,我会执行搜索以显示带有TextField的第二个ViewController。 现在,我想将信息写入TextField并将该信息链接到第一个ViewController的选定行。因此,单击行1时将显示Information1,单击行2时将显示Information2,依此类推。 我不希望ViewItems和Items一样多,所以我想知道最好的解决方法是什么?

3 个答案:

答案 0 :(得分:0)

您只需1个vc作为segue的目的地,即可使用通过prepare方法设置的变量

let arr = [YourModel(name:"ppp",age:11),YourModel(name:"ppp",age:14)]

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "YourSegue", sender: arr[indexPath.row])
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "YourSegue" {
        let des = segue.destination as! DetailVC
        des.item = sender as! YourModel

    } 
}

struct YourModel {
    let name: String
    let age: Int 
}

class DetailVC:UIViewController { 
   var item:YourModel?
}

答案 1 :(得分:0)

在目标View Controller中声明全局PHP Fatal error: Uncaught SoapFault exception: [SOAP-ENV:Client] error in msg parsing:\nXML error parsing SOAP payload on line 2: EntityRef: expecting ';'

在您的parentViewController的public var content:String?中,在执行segue之前写以下代码:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

这将使用预加载的数据执行搜索

答案 2 :(得分:0)

据我了解,您的问题更多是关于如何设计您的应用程序,以使其易于维护。这是我的建议:

//1. Create a structure to hold the data
struct Information{
    let cellTitle : String
    let messageToDisplay : String

    init(cellTitle: String, messageToDisplay: String){
        self.cellTitle = cellTitle
        self.messageToDisplay = messageToDisplay
    }
}

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    //2. Create a datasource array
    let datasource : [Information] = [Information(cellTitle: "Cell1", messageToDisplay: "Message To Display on for Cell1"),
                                    Information(cellTitle: "Cell2", messageToDisplay: "Message To Display on for Cell2"),
                                    Information(cellTitle: "Cell3", messageToDisplay: "Message To Display on for Cell3")]

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

extension ViewController : UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
        performSegue(withIdentifier: "ShowSecondViewController", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let indexPath = tableView.indexPathForSelectedRow

        if segue.identifier == "ShowSecondViewController" {
            let vc = segue.destination as! SecondViewController
            //3.0 Create a variable in your destination Viewcontroller and set it here
            vc.messageToShow = datasource[(indexPath?.row)!].messageToDisplay

        }
    }
}
相关问题