我的tableview按name属性排序。所以,如果我输入名字"Hanna“,那么"Belle”tableview看起来会是:
贝儿
汉娜
但是当我选择一个单元格时,来自另一个单元格的数据会显示出来,因为我输入了它的顺序。我怎么才能解决这个问题?
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
}如果需要更多的信息,请告诉我,我会编辑我的问题。
发布于 2019-12-07 00:20:05
在cellForRowAt内部使用fetchedResultsController
let pet = fetchedResultsController.object(at: indexPath)但是在didSelectRowAt内部使用pets
vc.pet = pets[indexPath.row]在这两种情况下都必须使用相同的数组。
https://stackoverflow.com/questions/59221794
复制相似问题