【发布时间】:2015-04-21 23:05:18
【问题描述】:
我是 Swift 和 IOS 的新手,我曾经有一个普通的表格视图,一切正常。我现在已经实现了一个自定义表格视图单元格,并且想知道如何使用我的 UITableView 实现我的 PrepareForSegue 方法。我希望能够将选定的表格单元索引发送到 segue,以便下一个控制器访问某个数组位置。现在发送者对象是一个 CustomCell: UITableViewCell 对象。我可以从该对象或其他方式访问表索引吗?
//
// ViewController.swift
// OBU Bus Tracker
//
// Created by AJ Norton on 4/20/15.
// Copyright (c) 2015 AJ Norton. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITableViewDelegate {
@IBOutlet var table: UITableView!
var locations = [String]()
var overall = Dictionary<String, AnyObject>()
override func viewDidLoad() {
super.viewDidLoad()
// check if the user is running the app for the first time
// if let firstTime = NSUserDefaults.standardUserDefaults().objectForKey("firstTime")
// {
//
// println("second time")
// }
// else
// {
// println("worked")
// NSUserDefaults.standardUserDefaults().setBool(true, forKey: "firstTime")
// }
if let path = NSBundle.mainBundle().pathForResource("busSchedule", ofType: "plist")
{
if let dict = NSDictionary(contentsOfFile: path) as? Dictionary<String, AnyObject>
{
locations = dict.keys.array
overall = dict
}
}
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return locations.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell: CustomRouteViewCell = table.dequeueReusableCellWithIdentifier("Cell") as! CustomRouteViewCell
cell.locationTitle.text = "\(locations[indexPath.row])"
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
println("I was clicked")
performSegueWithIdentifier("routeToTime", sender: indexPath.row)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//TODO
println("\(sender?.integerValue)")
if segue.identifier == "routeToTime"
{
var ttvc = segue.destinationViewController as! TimeViewController
var s = sender as! CustomRouteViewCell
println("\(s.in)")
var place = s.indentationLevel as! Int
var dicts = overall[locations[place]] as! Dictionary<String,AnyObject>
var arr = dicts["Weekday"] as! [Int]
ttvc.days = dicts
ttvc.times = arr.reverse()
}
}
}
【问题讨论】:
-
你的segue是用cell还是controller做的?如果发件人是单元格,这听起来像是来自单元格,但在这种情况下,您不应该在 didSelectRowAtIndexPath 中调用 performSegue(实际上您根本不需要实现该方法)。
-
那是未知的我不知道segue是在哪里制作的。在我的 prepareForSegue 方法中不再调用它。我不知道在哪里调用了 segue。
-
你没有做segue吗?制作时是从单元格中拖动还是从控制器图标中拖动?
标签: ios iphone uitableview swift