问题描述
我有两个视图控制器,分别是 UITableViewController
和 DestinationViewController
.DestinationViewController
的内容应该根据点击的 UITableViewCell 改变.我尝试了 this 教程,但这个教程没有意义.如何确定点击了哪个单元格以及如何在点击单元格时执行 segue?
I have two view controllers as UITableViewController
and DestinationViewController
. The content of DestinationViewController
should change according to tapped UITableViewCell. I tried this tutorial but this one didn't make sense. How can I determine which cell is tapped and how can I perform segue when the cell tapped?
UITableViewController:
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
}
目标视图控制器:
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
}
推荐答案
您可以在 tableView didSelectRowAtIndexPath 中执行 segue.然后在 prepareForSegue 方法中设置你的目标VC并传递所需的数据.
You can perform a segue in tableView didSelectRowAtIndexPath. Then in prepareForSegue method set your destinationVC and pass needed data.
代码:
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 并设置标识符.如果您有任何问题,请在评论中提出.
Don't forget to create a segue and set the identifier. If you have questions ask in comments.
这是您的 tableView didSelectRowAtIndexPath:
Here is your 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)
}
这篇关于如何根据点击的 UITableCell 更改目标视图控制器的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!