如何根据点击的UITableCell更改目标视图控制器的内容?

时间:2015-09-13 09:15:23

标签: ios swift uitableview segue

我有两个视图控制器UITableViewControllerDestinationViewControllerDestinationViewController的内容应根据点击的UITableViewCell进行更改。我试过this教程,但这个没有意义。如何确定轻敲哪个单元格?如何在单元格轻敲时执行segue?

修改

的UITableViewController:

let days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

var myIndex: Int?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    table1View.deselectRowAtIndexPath(indexPath, animated: true)
    myIndex = indexPath.row
    self.performSegueWithIdentifier("showClassesForSchedule", sender: nil)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let identifier = segue.identifier {
        switch identifier {
        case "showClassesForSchedule":
            let destinationViewController = segue.destinationViewController as? ScheduleAsClassTableViewController// set here нour destionation VC
            if destinationViewController != nil {
                destinationViewController?.tableArray.append(days[myIndex!])
                destinationViewController?.tableArray = days
                print(days[myIndex!])
            }
        default: break
        }
    }
}


func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return days.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("scheduleDaysTextCellIdentifier", forIndexPath: indexPath) as UITableViewCell

    let row = indexPath.row
    cell.textLabel?.text = days[row]

    return cell
}

DestinationViewController:

    var tableArray = [String]()
    @IBOutlet var table1View: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        print(tableArray)
    }
    @IBOutlet weak var comingLabelAsNormalView: UILabel!

    @IBOutlet weak var comingName: UILabel!

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

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return tableArray.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = table1View.dequeueReusableCellWithIdentifier("scheduleDayClassIdentifier", forIndexPath: indexPath) as UITableViewCell

        let row = indexPath.row
        cell.textLabel?.text = tableArray[row]

        return cell
    }

2 个答案:

答案 0 :(得分:1)

您可以在tableView didSelectRowAtIndexPath中执行segue。然后在prepareForSegue方法中设置destinationVC并传递所需的数据。

代码:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let identifier = segue.identifier {
        switch identifier {
        case "yourSegueIdentifier":
            let destinationViewController = segue.destinationViewController as? UIViewController // set here нour destionation VC
            if destinationViewController != nil {
                destinationViewController?.passedData = yourData
            }
        default: break
        }
    }
}

不要忘记创建一个segue并设置标识符。如果您有任何问题请在评论中提出。

这是你的tableView didSelectRowAtIndexPath:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    // save indexPath to any Int variable
    myIndex = indexPath.row
    self.performSegueWithIdentifier("yourSegueIndentifier", sender: nil)
}

答案 1 :(得分:1)

有几种方法可以做到这一点。我推荐使用segues。此方法需要didSelectRowAtIndexPath

首先,通过右键单击并从原型单元格拖动到下一个视图控制器并选择Show来创建一个segue:

enter image description here

然后给segue一个标识符:

enter image description here

接下来,您需要引用已点击的特定单元格的索引路径。您可以在prepareForSegue中执行此操作。尝试这样的事情:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "detail" {
        if let cell = sender as? UITableViewCell{
            if let indexPath = self.tableView.indexPathForCell(cell) {
                // get a reference to the next view controller so you can pass data in
                var nvc = segue.destinationViewController as! NewViewController

                // set properties on the next view controller using the acquired cell's index
                nvc.name = names[indexPath.row]
            }
        }
    }
}

上面的代码检查segue标识符。在这种情况下,sender是用户点击的UITableViewCell。接下来的两行尝试获取对此单元及其关联的indexPath的引用。然后,您可以在segue之前使用indexPath.rowindexPath.section来访问相关数据。然后获取一个引用到下一个视图控制器(您应该为其创建一个关联的类并将其连接到接口构建器中),然后可以在此引用上设置您想要设置的任何属性。