gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:23:37 25 4
gpt4 key购买 nike

我的 tableviewname 属性排序。因此,如果我输入名称“Hanna”,然后输入“Belle”,tableview 将如下所示:


Belle


Hanna


但是当我选择一个单元格时,另一个单元格的数据会显示,因为我猜是因为我输入它的顺序。我该如何解决这个问题?

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

PetSelectedViewController.swift

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)

但是在 didSelectRowAt 中你使用 pets

vc.pet = pets[indexPath.row]

你必须在两者中使用相同的数组

关于ios - 如何在 didSelectRowAt indexPath 中选择正确的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59221794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com