gpt4 book ai didi

swift - 如何将数据从 tableview 传递到不同的 uiviewcontroller

转载 作者:行者123 更新时间:2023-11-28 11:04:38 27 4
gpt4 key购买 nike

我正在编写一个应用程序,该应用程序将包含一个包含天数列表的 TableView 。单击一天后,我想显示一个页面,其中包含带有文本的信息和每天唯一的按钮。

我计划创建一个特定于每一天的不同 View Controller 。但是,我不知道如何将数据从每天的 TableView 传递到所选特定日期的特定 View Controller 。

最佳答案

您可以在表格 View 中使用 UITableView 委托(delegate)方法处理 click 事件您需要实现 UITableViewDelegate。要将数据传递给特定的 View Controller ,您可能需要使用 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
}
}

希望对您有所帮助!

关于swift - 如何将数据从 tableview 传递到不同的 uiviewcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38733133/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com