get Entity Index-row in Core Data

Discussion in 'iOS Programming' started by Zelda, Nov 14, 2014.

  1. Zelda, Nov 14, 2014
    Last edited: Nov 17, 2014

    Zelda macrumors member

    Joined:
    Aug 10, 2014
    #1
    Hello ,
    I'm using latest iOS 8 SDK and Swift.
    How can i get the Entity Index-row or section property when i select from a tableview?
    I tried the following,
    Code:
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
            if segue.identifier == "showDetail" {
    
                if let indexPath = self.tableView.indexPathForSelectedRow() {
    
    	    let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as Event
    
                        }
    
                   (segue.destinationViewController as DetailViewController).detailItem = object
    
                    }
    How would i retrieve the index row or section for the core data entity ?..
    Any hints or advice is appreciated.
    Thanks
    Z
     
  2. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #2
    Not sure how you do it in Swift, but you can create an NSArray of your Core Data entries using NSFetchRequest and executeFetchRequest. After that you should be able to load that into a table view and select your desired index.
     
  3. Zelda thread starter macrumors member

    Joined:
    Aug 10, 2014
    #3
    Thanks for the reply,
    It is not quite what i need, the tableview is already populated in the masterView.
    When i tap on the tableview and seque to the detailView i can send the specific entity's attribute's over to the detailView, the problem is in the detailView.
    I want to edit the attribute and save or update the Model with the following code:
    Code:
    	let fetchRequest = NSFetchRequest(entityName: "Event")
    
            if let fetchResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [Event] {        
    
                    fetchResults[0].attribute1 = value1
    
                    fetchResults[0].attribute2 = value2
    
              // i need the index row for the array fetchResults[0].attribute1 = value1  
    
    }            
    
    
    I need to grab the index row for the entity record that the user taps on the tableView in the MasterViewController.
    Thanks
    Z
     
  4. Zelda, Nov 18, 2014
    Last edited: Nov 18, 2014

    Zelda thread starter macrumors member

    Joined:
    Aug 10, 2014
    #4
    I found something that might work, just haven't got it running yet.
    Code:
    if let indexPath = self.tableView.indexPathForSelectedRow() {
                   
                    let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as Event
                    
                     let indexPathEntity = object.indexOfAccessibilityElement(managedObjectContext?)
                    
    
    So the first line gets the index of the tableView cell selected.
    And the second line gets the Entity core data record for that tableView cell index.
    I need the third line to grab the index of the Entity the user selected, to use in the detail view.
    thanks
    Z
     

Share This Page