当我单击TableView中的单元格时如何显示新的详细信息单元格?

时间:2019-12-22 15:39:06

标签: ios swift uitableview

我正在制作有关AutoChess的应用程序。在TableView中,我显示所有英雄。我想单击英雄单元格,然后它可以显示一个新单元格,该单元格具有有关该英雄的一些统计信息。这是我想要的代码和图像。感谢您的帮助

ViewController

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
}

}

This is i want my table look like

2 个答案:

答案 0 :(得分:1)

This is the log message

2019-12-23 08:00:03.755[0;39m [32m INFO[0;39m [35m3392[0;39m [2m---[0;39m [2m[hresholdUpdater][0;39m [36mc.n.e.r.PeerAwareInstanceRegistryImpl   [0;39m [2m:[0;39m Current renewal threshold is : 8
[2m2019-12-23 08:01:03.738[0;39m [32m INFO[0;39m [35m3392[0;39m [2m---[0;39m [2m[a-EvictionTimer][0;39m [36mc.n.e.registry.AbstractInstanceRegistry [0;39m [2m:[0;39m Running the evict task with compensationTime 0ms
[2m2019-12-23 08:02:03.738[0;39m [32m INFO[0;39m [35m3392[0;39m [2m---[0;39m [2m[a-EvictionTimer][0;39m [36mc.n.e.registry.AbstractInstanceRegistry [0;39m [2m:[0;39m Running the evict task with compensationTime 0ms
[2m2019-12-23 08:03:03.739[0;39m [32m INFO[0;39m [35m3392[0;39m [2m---[0;39m [2m[a-EvictionTimer][0;39m [36mc.n.e.registry.AbstractInstanceRegistry [0;39m [2m:[0;39m Running the evict task with compensationTime 0ms
[2m2019-12-23 08:04:03.739[0;39m [32m INFO[0;39m [35m3392[0;39m [2m---[0;39m [2m[a-EvictionTimer][0;39m [36mc.n.e.registry.AbstractInstanceRegistry [0;39m [2m:[0;39m Running the evict task with compensationTime 0m

基本上是这样,只是当您从类中调用全局变量时,您可以像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希望它能对您有所帮助! !

答案 1 :(得分:0)

tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)函数,例如,通过设置选定标志,为tableHero -array中的选定索引更新对象。然后在表格视图上调用reloadRows(at indexPaths: [IndexPath], with animation: UITableView.RowAnimation)以重新加载单元格。

您还可以在当前IndexPath下插入一个新单元格。在tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)函数中,创建一个新对象并将其添加到tableHero -array中,并通过调用:insertRows(at indexPaths: [IndexPath], with animation: UITableView.RowAnimation)将其插入所选单元格的下方。