我有以下问题: 我正在制作一个类似于 Pokédex 的应用程序,它在第一个选项卡上显示所有 721 只 Pokémon 的列表,在第二个选项卡上显示另一个包含我最喜欢的 Pokémon 的列表。本质上,有两个相同的 ViewController 连接到我的 TabBar。
问题来了: 第一个(和初始)选项卡上的 TableView 工作正常。但是,当我在第二个选项卡上加载 TableView 时,Pokémon 已加载,但未显示。 我可以单击 TableViewCell 并转到详细信息页面,但 TableViewCell 中的标签没有显示任何内容。
这是我用来加载收藏夹 TableView 的代码
class FavoritesViewController: BaseViewController,
UITableViewDataSource, UITableViewDelegate {
@IBOutlet var FavoritesListView: UITableView!
var pokemonList: [String] = ["Nothing Here!"]
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("FavoriteCell", forIndexPath: indexPath) as! FavoriteCell
var name = pokemonList[indexPath.row]
capitalizeFirstLetter(&name)
cell.nameLabel.text = name
return cell;
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pokemonList.count
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print(pokemonList[indexPath.row])
self.performSegueWithIdentifier("ToPokemonDetail", sender: pokemonList[indexPath.row])
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "ToPokemonDetail"){
let destination = segue.destinationViewController as! PokemonDetailViewController
let thisPokemon = sender as! String
destination.currentPokemon = thisPokemon
}
}
override func viewWillAppear(animated: Bool) {
FavoritesListView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
// Fetch the cached list, getNames returns an array of strings
let list = utility.getNames("Favorites")
pokemonList = list
}
委托(delegate)和数据源是通过 Storyboard设置的。
上面的代码可以正常显示收藏夹列表。完整 Pokédex 的类具有类似的结构。
我试过切换收藏夹和 Pokédex,以便它在启动时显示完整的 Pokémon 列表。所有 721 只神奇宝贝都正确显示,但收藏夹不可见。
我还尝试了什么:
- 一遍又一遍地检查重用标识符
- 引用网点应正确绑定(bind)
- 在 viewDidAppear 方法中调用 TableView.reloadData()
- 切换标签项
有人知道这里到底发生了什么吗? 有任何问题欢迎随时提问
编辑:这是我交换两个 TabBar 按钮时发生的情况,没有代码更改
最佳答案
问题出在 Storyboard单元格标签框架中。为 (Any,Any) Size Class 设置 View Controller 的约束。如果你能给我在你的 git 上的写权限,我可以在 github 上提交代码。谢谢
关于ios - UITableViewCells 未出现在第二个选项卡中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36556355/