Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在didSelectRowAt indexPath中选择正确的实体_Ios_Swift_Uitableview_Core Data_Didselectrowatindexpath - Fatal编程技术网

Ios 如何在didSelectRowAt indexPath中选择正确的实体

Ios 如何在didSelectRowAt indexPath中选择正确的实体,ios,swift,uitableview,core-data,didselectrowatindexpath,Ios,Swift,Uitableview,Core Data,Didselectrowatindexpath,我的tableview按name属性排序。因此,如果我输入名称“Hanna”,则“Belle”的表视图将如下所示: 美女 汉娜 但当我选择一个单元格时,来自另一个单元格的数据会显示出来,因为我猜是按顺序输入的。我怎样才能解决这个问题 MainTableViewController.swift override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableVi

我的
tableview
name
属性排序。因此,如果我输入名称“Hanna”,则“Belle”的
表视图将如下所示:


美女


汉娜


但当我选择一个单元格时,来自另一个单元格的数据会显示出来,因为我猜是按顺序输入的。我怎样才能解决这个问题

MainTableViewController.swift

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? MainTableViewCell

    let pet = fetchedResultsController.object(at: indexPath)

    cell?.nameLB.text = pet.name
    if pet.isDog {
        cell?.isDogString = "dog"
        cell?.petImage.image = UIImage(named: "dog.png")
    } else {
        cell?.isDogString = "cat"
        cell?.petImage.image = UIImage(named: "cat.png")
    }
    if pet.isBoy {
        cell?.backgroundColor = UIColor(red: 102/255, green: 230/255, blue: 255/255, alpha: 1.0)
    } else {
        cell?.backgroundColor = UIColor(red: 255/255, green: 153/255, blue: 255/255, alpha: 1.0)
    }
    return cell!
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vc = storyboard?.instantiateViewController(withIdentifier: "PetSelectedViewController") as! PetSelectedViewController
    vc.pet = pets[indexPath.row]
    _ = navigationController?.pushViewController(vc, animated: true)
}
var pet: Pet! // entity

override func viewDidLoad() {
    super.viewDidLoad()
    basicPetInfoUI()
    loadData()
}

func loadData() {
    var isBoyString = ""
    var isVaccString = ""
    pet!.isBoy ? (isBoyString = "Boy") : (isBoyString = "Girl")
    pet!.isVaccinated ? (isVaccString = "Vaccinated") : (isVaccString = "Not Vaccinated")
    nameLabel.text = pet.name
    breedLabel.text = pet.breed
    isBoyLabel.text = isBoyString
    isVaccinatedLabel.text = isVaccString
    weightLabel.text = pet.weight
}
PetSelectedViewController.swift

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? MainTableViewCell

    let pet = fetchedResultsController.object(at: indexPath)

    cell?.nameLB.text = pet.name
    if pet.isDog {
        cell?.isDogString = "dog"
        cell?.petImage.image = UIImage(named: "dog.png")
    } else {
        cell?.isDogString = "cat"
        cell?.petImage.image = UIImage(named: "cat.png")
    }
    if pet.isBoy {
        cell?.backgroundColor = UIColor(red: 102/255, green: 230/255, blue: 255/255, alpha: 1.0)
    } else {
        cell?.backgroundColor = UIColor(red: 255/255, green: 153/255, blue: 255/255, alpha: 1.0)
    }
    return cell!
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vc = storyboard?.instantiateViewController(withIdentifier: "PetSelectedViewController") as! PetSelectedViewController
    vc.pet = pets[indexPath.row]
    _ = navigationController?.pushViewController(vc, animated: true)
}
var pet: Pet! // entity

override func viewDidLoad() {
    super.viewDidLoad()
    basicPetInfoUI()
    loadData()
}

func loadData() {
    var isBoyString = ""
    var isVaccString = ""
    pet!.isBoy ? (isBoyString = "Boy") : (isBoyString = "Girl")
    pet!.isVaccinated ? (isVaccString = "Vaccinated") : (isVaccString = "Not Vaccinated")
    nameLabel.text = pet.name
    breedLabel.text = pet.breed
    isBoyLabel.text = isBoyString
    isVaccinatedLabel.text = isVaccString
    weightLabel.text = pet.weight
}

如果需要更多信息,请告诉我,我将编辑我的问题。

cellForRowAt
中使用
fetchedResultsController

let pet = fetchedResultsController.object(at: indexPath)
但是在
里面你选择了rowat
你使用的
宠物

vc.pet = pets[indexPath.row]

您必须在两个中使用相同的数组设置
var pet:pet?能否添加
cellForRow
?didSelectRowAt和added cellForRow中的固定行