如何将数据从tableview传递到另一个uiviewcontroller

时间:2016-08-03 01:58:17

标签: swift xcode tableview

我正在编写一个包含tableview的应用程序,其中包含一个天数列表。点击一天后,我想显示一个页面,其中包含文字信息和每天独有的按钮。

我计划创建一个特定于每天的不同视图控制器。但是,我不知道如何将每天从tableview传递的数据传递到所选特定日期的特定视图控制器。

3 个答案:

答案 0 :(得分:3)

您可以在tableview中使用UITableView委托方法进行点击活动 您需要实施UITableViewDelegate。要将数据传递到特定视图控制器,您可能需要使用prepareForSegue函数

var day = [1,2,3,4,5]
var selected_day : Int = 0
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("yourcellidentifier") as! yourtableViewCell
        cell.labelday.text = self.day[indexPath.row]// just sample
        return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //this method will be called when you click 1 of the row from tableview
        self.selected_day = self.day[indexPath.row]
        self.performSegueWithIdentifier("ToYourSpecificViewController", sender: self) // you have to link with your table view controller and your specific view controller with an identifier.  
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
         if segue.destinationViewController is YourSpecificViewController{
            let vc = segue.destinationViewController as! YourSpecificViewController
            // In YourSpecificViewController, you also need to declare a variable name called selected_day to catch 
            vc.selected_day = self.selected_day
         }
    }

希望这有帮助!

答案 1 :(得分:0)

在包含该表的视图控制器中,实现prepareforsegue()方法:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

    let row = self.tableView.indexPathForSelectedRow?.row

    if segue.identifier == "Sunday" {
        let vc = segue.destinationViewController as! SundayViewController
        vc.myInt = dataModel[row!].theInt  // This changes depending on how your data is set up and whether you're grabbing the info from a text field, or what have you
    }

    else if segue.identifier == "Monday" {   
        let vc = segue.destinationViewController as! MondayViewController
        vc.myInt = dataModel[row!].theInt
        vc.someString = dataModel[row!].theString
    }
}

日子'视图控制器看起来像:

class SundayViewController: UIViewController {

    var myInt: Int?
    // etc
}

class MondayViewController: UIViewController {

    var myInt: Int?
    var someString: String?
    // etc
}

答案 2 :(得分:0)

在tableviewcontroller中实现此代码

class TableviewController: UITableViewController {

    var array : [DayObject]? = [DayObject(day: "Sunday", daytext: "SundayText"),DayObject(day: "Monday", daytext: "MondayText"),DayObject(day: "tuesday", daytext: "TuesdayText"),DayObject(day: "Wednesday", daytext: "WednesdayText")]
    var object: DayObject?

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

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }
}

extension TableviewController {


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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell")
        cell?.textLabel?.text = array![indexPath.row].day
        return cell!
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        object =  array![indexPath.row]
        performSegueWithIdentifier("NVC", sender: self)

    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "NVC" {
            let dvc = segue.destinationViewController as? ViewController2
            dvc!.object = object
        }
    }

}

并制作如下数据模型:

import UIKit
class DayObject: NSObject {

    var day: String!
    var daytext: String!


    init(day: String, daytext: String) {
        self.day = day
        self.daytext =  daytext
    }
}

并在视图控制器中,您可以收集对象

class ViewController2: UIViewController {
    var object: DayObject!

    override func viewDidLoad() {
        super.viewDidLoad()
        print(object.daytext)
    }
}

通过datamodel方法,您不必每天制作不同的视图控制器

happycoding: - )