Search code examples
swiftipadios8uisplitviewcontroller

swift splitview controller default cell


I am currently working on a project with a split view controller using swift in xcode6.

I currently have it so that when the app loads it remembers which cell you previously had selected and will "select" that cell. However when the cell is selected it doesn't initiate the segue to the detail view controller loading that cells related information.

This is my master view controller code, wondering what I can add to initate the segue for the selected cell when the app loads?

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)

}

Solution

  • You can initiate a segue manually with the performSegueWithIdentifier method on UIViewController.

    // in your dispatch_async block:
    self.performSegueWithIdentifier("showDetail", self)