首页 > 解决方案 > 当我单击 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
}

}

这是我希望我的桌子看起来像

标签: iosswiftuitableview

解决方案


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]希望这会有所帮助!


推荐阅读