【发布时间】:2015-06-30 08:30:38
【问题描述】:
我目前正在开发一个在 xcode6 中使用 swift 的带有拆分视图控制器的项目。
我目前拥有它,以便在应用加载时记住您之前选择的单元格,并将“选择”该单元格。 However when the cell is selected it doesn't initiate the segue to the detail view controller loading that cells related information.
这是我的主视图控制器代码,想知道我可以添加什么来在应用加载时为所选单元格启动 segue?
class MasterViewController: UITableViewController {
var detailViewController: DetailViewController? = nil
var titles = [String]()
var ageGroups = [String]()
var danceForms = [String]()
var danceDivisions = [String]()
var danceCategories = [String]()
var routineIds = [Int]()
override func awakeFromNib() {
super.awakeFromNib()
self.clearsSelectionOnViewWillAppear = false
self.preferredContentSize = CGSize(width: 320.0, height: 600.0)
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.setRightBarButtonItems([rightAddBarButtonItem,rightSearchBarButtonItem], animated: true)
let query = PFQuery(className: "Schedule")
query.orderByAscending("routineId")
query.whereKey("eventId", equalTo: 16)
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let objects = objects {
for object in objects {
self.titles.append(object["title"] as! String)
self.ageGroups.append(object["ageGroup"] as! String)
self.danceForms.append(object["danceForm"] as! String)
self.danceDivisions.append(object["danceDivision"] as! String)
self.danceCategories.append(object["danceCategory"] as! String)
self.routineIds.append(object["routineId"] as! Int)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
var returnValue: Int? = NSUserDefaults.standardUserDefaults().objectForKey("selectedCell") as? Int
if let value = returnValue{
}
else{
returnValue = 1;
}
if returnValue! > self.routineIds.count{
returnValue = 1;
}
let rowToSelect:NSIndexPath = NSIndexPath(forRow: returnValue!-1, inSection: 0); //slecting 0th row with 0th section
self.tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition: UITableViewScrollPosition.None);
})
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Segues
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = self.tableView.indexPathForSelectedRow() {
let object = routineIds[indexPath.row] as Int
let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
// MARK: - Table View
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titles.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("scheduleCell", forIndexPath: indexPath) as! scheduleCell
var myCustomSelectionColorView = UIView()
myCustomSelectionColorView.backgroundColor = UIColor.whiteColor()
cell.selectedBackgroundView = myCustomSelectionColorView
cell.routineId.text = "\(routineIds[indexPath.row])"
cell.routineTitle.text = titles[indexPath.row]
cell.routineAgeGroup.text = " - \(ageGroups[indexPath.row])"
cell.routineDanceForm.text = " - \(danceForms[indexPath.row])"
cell.routineDivision.text = danceDivisions[indexPath.row]
cell.routineCategory.text = danceCategories[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return false
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println(routineIds[indexPath.row])
NSUserDefaults.standardUserDefaults().setInteger(routineIds[indexPath.row], forKey: "selectedCell")
NSUserDefaults.standardUserDefaults().synchronize()
var returnValue: Int? = NSUserDefaults.standardUserDefaults().objectForKey("selectedCell") as? Int
if returnValue! == 1{
}
println(returnValue)
}
【问题讨论】:
标签: swift ipad ios8 uisplitviewcontroller