ios - 当我单击 TableView 中的单元格时,如何显示新的详细信息单元格?
问题描述
我正在制作一个关于 AutoChess 的应用程序。TableView,我尽显英雄本色。我想点击英雄单元格,然后它可以显示一个新单元格,这个单元格有一些关于那个英雄的统计数据。这是我希望它看起来像的代码和图像。谢谢你的帮助
视图控制器
extension HeroesViewController : UITableViewDelegate, UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return tableHero.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableHero[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tableSection = tableView.dequeueReusableCell(withIdentifier: "HeroViewCell", for: indexPath) as! HeroViewCell
let heroObj = tableHero[indexPath.section]
tableSection.configCell(obj : heroObj, indexPath : indexPath)
return tableSection
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = .clear
}
} HeroViewCell
class HeroViewCell: UITableViewCell {
@IBOutlet weak var imgAvatar: UIImageView!
@IBOutlet weak var lblName: UILabel!
@IBOutlet weak var lblBuff: UILabel!
@IBOutlet weak var imgAbility: UIImageView!
@IBOutlet weak var lblCost: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func configCell(obj : [Hero], indexPath : IndexPath){
self.imgAvatar.image = UIImage(named: obj[indexPath.row].avatar!)
self.lblName.text = obj[indexPath.row].nameHero
self.lblBuff.text = obj[indexPath.row].races?.joined(separator: " \n ")
if let imgAblityName = obj[indexPath.row].ability?.imageAbility{
self.imgAbility.image = UIImage(named: imgAblityName)
}
self.lblCost.text = obj[indexPath.row].cost
}
}
解决方案
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let Storyboard = UIStoryboard(name: "nameOfStoryboard", bundle: nil)
let DvC = Storyboard.instantiateViewController(withIdentifier: "UIViewControllerIdentifer") as! className
navigationController?.pushViewController(DvC, animated: true)
}
基本上是这样的,就在你从类中调用全局变量时,你可以像引用它一样引用它,DvC.varableName = array[indexPath.row].nameOfStoredVarible
或者如果你没有排序变量,你可以这样做DvC.varableName = array[indexPath.row]
希望这会有所帮助!
推荐阅读
- selenium - 如何在没有服务器的情况下使用 selenium 自动化 android 应用程序
- java - 正则表达式匹配以逗号分隔的字符串中的元素,忽略双引号、大括号和方括号内的逗号
- laravel-8 - laravel8中资源控制器中的数组到字符串转换错误
- python-3.x - 如何测试保存在我的文件夹中的所有图片不仅仅是一张
- python - Python如何在不干扰的情况下每n秒运行多个函数
- google-apps-script - Google 表格到文件夹内部的 Pdf 并管理 Pdf 所有权
- c# - 当表单超过错误登录尝试次数时,我似乎无法关闭表单
- arrays - C - 头文件中的变量无法识别
- sql - How to get JSON string for each distinct field
- c# - ASP.NET Core 路由返回 404