添加信息/详细到TableView中排

问题描述 投票:0回答:3

我有一个在第一的ViewController项的TableView。当我点击某一行上我执行赛格瑞上有一个文本字段显示第二视图控制器。现在,我想编写信息的文本字段,并从第一个视图控制器的信息链接到选定行。使显示信息1,当您单击ROW1,当你点击2行等信息2所示。我不想有许多ViewControllers的项目,所以我想知道什么是最好的办法是解决这个问题?

swift segue viewcontroller
3个回答
0
投票

在您的目的地视图控制器声明一个全局public var content:String?

在parentViewController的func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath),执行赛格瑞前这样写:

let destVC = YourDestinationVC()
destVC.content = "content pertaining to cell selected"

// if the segue name is called segueOne
destinationVC.performSegueWithIdentifier("segueOne", sender: self)

这将执行与预加载数据的SEGUE


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

        }
    }
}

0
投票

你只需要1个VC为具有可变赛格瑞的目的地,你从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?
}
© www.soinside.com 2019 - 2024. All rights reserved.