【问题标题】:Cannot call value of non-function type 'UITableView无法调用非函数类型“UITableView”的值
【发布时间】:2016-11-19 22:52:59
【问题描述】:

我试图在静态单元格中弹出日期选择器,但出现此编译器错误。有什么想法吗?

在线出错:“return super.tableView(tableView, heightForRowAtIndexPath: indexPath)”

import UIKit

class TableViewController: UITableViewController {

@IBOutlet weak var detailLabel: UILabel!
@IBOutlet weak var datePicker: UIDatePicker!
var datePickerHidden = false

override func viewDidLoad() {
    super.viewDidLoad()
    datePickerChanged()
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

@IBAction func datePickerValue(_ sender: UIDatePicker) {
    datePickerChanged()
}

func datePickerChanged () {
    detailLabel.text = DateFormatter.localizedString(from: datePicker.date, dateStyle: DateFormatter.Style.short, timeStyle: DateFormatter.Style.short)
}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.section == 0 && indexPath.row == 0 {
        toggleDatepicker()
    }
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if datePickerHidden && indexPath.section == 0 && indexPath.row == 1 {
        return 0
    }
    else {
        return super.tableView(tableView: tableView, didSelectRowAtIndexPath: indexPath)
    }
}

func toggleDatepicker() {

    datePickerHidden = !datePickerHidden

    tableView.beginUpdates()
    tableView.endUpdates()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

我应该如何解决这个问题?

【问题讨论】:

    标签: ios swift uitableview datepicker tableview


    【解决方案1】:

    假设您在 UITableViewController 类中调用它。好吧,UITableViewController 有一个名为 tableView 的属性。因此,当您从 super.tableView 开始时,它认为您正在请求该属性,这就是为什么它会给出 tableView 是非函数类型的错误。

    查看return super.tableView.rowHeight 是否适合您的情况。

    【讨论】:

    • 错误消失并运行。但是,当我在日期选择器之间切换时,应用程序立即崩溃,并且在 AppDelgate 的这一行上出现“线程 1:信号 SIGABRT”错误:class AppDelegate: UIResponder, UIApplicationDelegate {
    • 有些东西可能是零;见stackoverflow.com/questions/32170456/…
    • 这可能与您的原始问题或此解决方案有关,也可能无关。如果我的回答解决了您最初的问题,如果您接受了答案,我将不胜感激。然后,您应该尝试获取有关您的新错误的详细信息并发布另一个问题,社区会尽力帮助您。
    猜你喜欢
    • 2017-06-27
    • 1970-01-01
    • 2017-08-16
    • 2020-02-01
    • 2021-01-15
    • 2021-03-30
    • 2016-03-10
    • 2016-09-26
    • 2019-05-05
    相关资源
    最近更新 更多