UITableView过滤的数据具有不正确的索引

时间:2015-10-01 14:20:23

标签: ios uitableview core-data swift2

这是我的代码,用于过滤我的CoreData中的数据,它提供了更正的数据,但如果我查看记录的详细信息,它会给我错误的数据。

含义我有详细的VC,可以显示更大的相机图像。因此,如果索引是带过滤数据的1,但索引1中相机的实际索引在CoreData中实际为20。我该如何纠正这个问题?

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;
}

更新
请求更新信息。

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searchActive {
        return filteredData.count
    } else {
    return myData.count
    }
}

下一项。

    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
}

下一项

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let selectedIndexPath:NSIndexPath = self.tableView.indexPathForSelectedRow!

    let vc = segue.destinationViewController as! largeCameraViewController
    vc.row = selectedIndexPath.row

}

1 个答案:

答案 0 :(得分:1)

您应该在prepareForSegue中传递对象本身,而不是行。将属性添加到LargeCameraViewController

var camera : NSManagedObject?

然后在prepareForSegue中,从camerafilteredData的相关行获取myData,具体取决于searchActive是真还是假:< / p>

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
    }
}

然后在LargeCameraViewController中,您可以从camera NSManagedObject访问相关属性。