【发布时间】:2017-01-23 13:39:16
【问题描述】:
我正在创建一个 pokedex 应用程序,我希望它的工作方式基本上是屏幕顶部有一个滚动条,可让您选择任何 pokemon,在选择 pokemon 时,滚动条下方是 pokemon 的条目会出现(bulbasaur 默认情况下会在那里,直到选择一个口袋妖怪,因为bulbasaur 是第一个 ID 为 1 的口袋妖怪)。为了实现这一点,我让视图控制器返回两种类型的单元格,第一种是作为滚动条的“选择器单元格”,第二个是作为 dex 条目的“描述单元格”。我给视图控制器一个名为 dex entry 的数据成员,并在 cellForItemAt 函数中返回 dex 条目,但单元格的图像没有改变(从bulbasaur 到我选择的任何口袋妖怪)。每次选择口袋妖怪时,我都会向控制台打印 dex 条目的口袋妖怪的值是多少,所以我确信 dex 条目正在被直接更改,但我不知道为什么图像也没有改变。以下是我的代码的相关部分。
视图控制器(只是它的一部分):
import UIKit
class PokeDexController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var dexEntry = DescriptionCell()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "PokeDex 386"
collectionView?.backgroundColor = UIColor(red: 52/255.0, green: 55/255.0, blue: 64/255.0, alpha: 1.0)
//collectionView?.backgroundColor = UIColor.white
collectionView?.register(chooserCell.self, forCellWithReuseIdentifier: cellID)
collectionView?.register(DescriptionCell.self, forCellWithReuseIdentifier: descID)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if (indexPath.row == 0)
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! chooserCell
return cell
}
else{
let descCell = collectionView.dequeueReusableCell(withReuseIdentifier: descID, for: indexPath) as! DescriptionCell
dexEntry = descCell
return dexEntry
}
}
描述Cell类:
import UIKit
class DescriptionCell: UICollectionViewCell
{
private var pokemon : Pokemon?
{
didSet
{
if let id = pokemon?._id
{
imageView.image = UIImage(named: String(id))
print("Pokemon with the id of " + String(id))
}
}
}
override init(frame: CGRect)
{
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setPokemon(poke: Pokemon)
{
self.pokemon = poke
}
func getPokemon() -> Pokemon
{
return pokemon!
}
let imageView: UIImageView =
{
let iv = UIImageView()
iv.image = UIImage(named: "1")
iv.contentMode = .scaleAspectFill
return iv
}()
func setupViews()
{
backgroundColor = UIColor(red: 52/255.0, green: 55/255.0, blue: 64/255.0, alpha: 1.0)
addSubview(imageView)
imageView.frame = (CGRect(x: frame.width/6, y: frame.height/30, width: frame.width/4, height: frame.height/4))
}
}
choosercell 类(特别是 didSelectItemAt):
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
let poke = pokemon[indexPath.row]
print("Selected " + poke._name)
let vc = PokeDexController()
vc.dexEntry.setPokemon(poke: poke)
let name = vc.dexEntry.getPokemon()._name
print(name ?? "nothing there")
}
image of the app and the console output
感谢任何帮助,谢谢。
【问题讨论】:
-
尝试将 collectionView.reloadItems(at indexPaths: [indexPath]) 添加到您的选择器单元类中的 didSelectItemAt
-
@chickenparm 你能解释一下这可能有什么帮助吗?
-
很遗憾没有帮助
标签: ios iphone swift uiviewcontroller uicollectionview