Reputation: 7168
This my code to filter data from my CoreData and it provides the corrected data but if I go to details of the record it gives me the wrong data.
Meaning I have detailed VC that shows a larger image of the camera. So if the index is lets say 1 with the filter data but the real index of the camera in index one is really 20 in CoreData. How can I correct for this issue?
func fetch() {
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
// Ref data
let context: NSManagedObjectContext = appDel.managedObjectContext!
let freq = NSFetchRequest(entityName: "Cameras")
let predicate = NSPredicate(format: "locationname == %@", "lobby")
freq.predicate = predicate
let fetchResults = try! context.executeFetchRequest(freq) as? [NSManagedObject]
filteredData.removeAll(keepCapacity: false)
filteredData = fetchResults!
searchActive = true;
}
UPDATE
Request updated information.
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchActive {
return filteredData.count
} else {
return myData.count
}
}
Next item.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cameraCell", forIndexPath: indexPath) as! camerasTableViewCell
if searchActive {
let data = filteredData[indexPath.row]
cell.nameLabel.text = data.valueForKey("name") as? String
cell.roadName.text = data.valueForKey("locationname") as? String
let CamImage = data.valueForKey("dImage") as! NSData
let CameraImage: UIImage = UIImage(data: CamImage)!
cell.imageUrl?.image = CameraImage
} else {
let data = myData[indexPath.row]
cell.nameLabel.text = data.valueForKey("name") as? String
cell.roadName.text = data.valueForKey("locationname") as? String
let CamImage = data.valueForKey("dImage") as! NSData
let CameraImage: UIImage = UIImage(data: CamImage)!
cell.imageUrl?.image = CameraImage
}
return cell
}
Next item
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let selectedIndexPath:NSIndexPath = self.tableView.indexPathForSelectedRow!
let vc = segue.destinationViewController as! largeCameraViewController
vc.row = selectedIndexPath.row
}
Upvotes: 0
Views: 109
Reputation: 21536
You should pass the object itself in prepareForSegue
, not the row. Add a property to your LargeCameraViewController
:
var camera : NSManagedObject?
Then in prepareForSegue
, obtain the camera
from the relevant row of either filteredData
or myData
, depending on whether searchActive
is true or false:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let selectedIndexPath:NSIndexPath = self.tableView.indexPathForSelectedRow!
let vc = segue.destinationViewController as! largeCameraViewController
if (searchActive) {
vc.camera = filteredData[selectedIndexPath.row] as! NSManagedObject
} else {
vc.camera = myData[selectedIndexPath.row] as! NSManagedObject
}
}
Then in LargeCameraViewController
you can access the relevant attributes from the camera
NSManagedObject.
Upvotes: 1