如何使用heightForRowAtIndexPath方法? [英] How to use the heightForRowAtIndexPath method?

查看:105
本文介绍了如何使用heightForRowAtIndexPath方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的UITableViewController中使用heightForRowAtIndexPath方法.但是,当我尝试覆盖该方法时,它说它没有覆盖其父类中的任何方法.

I am trying to use the heightForRowAtIndexPath method in my UITableViewController. But when I try to override the method it says that it does not override any methods from its superclass.

Google搜索使我想到了协议,这似乎可能是难题的一部分,但我仍在尝试了解如何使用协议来使用此方法.

Googling has led me to protocols, which seem to potentially be some part of the puzzle, but I'm still trying to understand how to use a protocol to use this method.

任何帮助将不胜感激.这是代码:(问题方法在最底部)

Any help would be greatly appreciated. Here's the code: (the problem method is at the very bottom)

import UIKit
import Firebase

class FruitsTableViewController: UITableViewController {
    let rootRef = FIRDatabase.database().reference()

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 15
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)

        let busRef = rootRef.child("buses")
        busRef.observeSingleEvent(of: .value, with: {(snapshot) in
            if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
                let bus = snapshots[Int(indexPath.row)].key
                let busval = snapshots[Int(indexPath.row)].value as! String
                cell.textLabel?.text = "Bus \(bus) ------- \(busval)"
            }
        })

        return cell
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "Section \(section)"
    }

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 40.0
    }
}

推荐答案

您可以设置静态高度:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    if indexPath.section == 0 {
        return 50
    } else {
        return 120
    }
}

或使用自动标注,根据其内容自动调整每个单元格的大小:

Or use automatic dimension, with resizes every cell automatically according to its content:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     return UITableViewAutomaticDimension
 }

这篇关于如何使用heightForRowAtIndexPath方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆