我正尝试在我的UITableViewController中使用heightForRowAtIndexPath方法。但是当我尝试覆盖这个方法时,它告诉我它没有覆盖它的超类中的任何方法。
谷歌搜索将我引向了协议,这似乎是问题的一部分,但我仍然在努力理解如何使用协议来使用这种方法。
任何帮助都将不胜感激。下面是代码:( problem方法在最底部)
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
}
}发布于 2017-02-17 12:38:37
您可以设置静态高度:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
if indexPath.section == 0 {
return 50
} else {
return 120
}
}或者使用自动尺寸标注,根据内容自动调整每个单元格的大小:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}发布于 2017-07-07 20:26:07
使用以下代码
func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return 50.0
}发布于 2017-02-17 12:03:10
您必须:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {将其替换为:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {我不确定你是否需要override,但是如果你需要,编译器会告诉你,你可以把它放回去。
https://stackoverflow.com/questions/42289097
复制相似问题