在尝试为iPhone使用Swift构建tableView时,我遇到了一个错误。

13

大家好,我看了很多教程,最终创建了一个面向应用商店的应用程序。最近,我对tableView产生了兴趣,所以我跟随了很多教程,但似乎总是出错,而在教程中的人却成功地构建了他们的应用程序。 当我尝试运行我的应用程序时,构建停止,并在AppDelegate类的“class AppDelegate…”这一行上收到一个异常,异常是“Thread 1: signal SIGABRT”。

对于那些感兴趣的人,这是控制台中的消息。

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'must pass a class of kind UITableViewHeaderFooterView'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000109c80f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010b7c4bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x0000000109c80e6d +[NSException raise:format:] + 205
    3   UIKit                               0x000000010a5fb254 -[UITableView registerClass:forHeaderFooterViewReuseIdentifier:] + 247
    4   tableviewtes                        0x0000000109a9cc3c _TFC12tableviewtes14ViewController11viewDidLoadfS0_FT_T_ + 460
    5   tableviewtes                        0x0000000109a9ccd2 _TToFC12tableviewtes14ViewController11viewDidLoadfS0_FT_T_ + 34
    6   UIKit                               0x000000010a631a90 -[UIViewController loadViewIfRequired] + 738
    7   UIKit                               0x000000010a631c8e -[UIViewController view] + 27
    8   UIKit                               0x000000010a550ca9 -[UIWindow addRootViewControllerViewIfPossible] + 58
    9   UIKit                               0x000000010a551041 -[UIWindow _setHidden:forced:] + 247
    10  UIKit                               0x000000010a55d72c -[UIWindow makeKeyAndVisible] + 42
    11  UIKit                               0x000000010a508061 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2628
    12  UIKit                               0x000000010a50ad2c -[UIApplication _runWithMainScene:transitionContext:completion:] + 1350
    13  UIKit                               0x000000010a509bf2 -[UIApplication workspaceDidEndTransaction:] + 179
    14  FrontBoardServices                  0x000000010d3512a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
    15  CoreFoundation                      0x0000000109bb653c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    16  CoreFoundation                      0x0000000109bac285 __CFRunLoopDoBlocks + 341
    17  CoreFoundation                      0x0000000109bac045 __CFRunLoopRun + 2389
    18  CoreFoundation                      0x0000000109bab486 CFRunLoopRunSpecific + 470
    19  UIKit                               0x000000010a509669 -[UIApplication _run] + 413
    20  UIKit                               0x000000010a50c420 UIApplicationMain + 1282
    21  tableviewtes                        0x0000000109a9faee top_level_code + 78
    22  tableviewtes                        0x0000000109a9fb2a main + 42
    23  libdyld.dylib                       0x000000010bf9e145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)  

2
使用“所有异常”断点运行应用程序,并向我们展示异常在代码中发生的位置。 - JAL
1
控制台消息似乎准确描述了问题所在。 - Robert Harvey
2
错误信息非常清晰 - 你传递给 registerClass:forHeaderFooterViewReuseIdentifier: 的类不是 UITableViewHeaderFooterView 的子类 - 检查你调用这个方法的位置以及你传递的类的定义。 - Paulw11
3个回答

40

你可能想要使用的函数是

tableView.registerClass(UITableViewController.self, forCellReuseIdentifier: cellIdentifier)

取代

tableView.registerClass(UITableViewController.self, forHeaderFooterViewReuseIdentifier: cellIdentifier)

希望这就是您需要的内容...


10

我认为这是一个微小的错误。

而不是:

tableView.register(UITableView.self, forCellReuseIdentifier: "cell")

你需要使用:

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
请注意,在注册函数中第一个参数应该是UITableViewCell而不是UITableView

编码愉快:)


0

我也曾经遇到过这个问题。

正如Paulw11所说,问题出在你的寄存器类方法调用上。

我按照下拉刷新教程操作,让表格视图像这样工作:

我进行了替换。

    var tableView = tableViewController.tableView

    tableView.registerClass(UITableViewController.self, forHeaderFooterViewReuseIdentifier: cellIdentifier)

使用:

var refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: Selector("sortArray"), forControlEvents: UIControlEvents.ValueChanged)
    self.refreshControl = refreshControl

...

func sortArray() {
    ...
}

启用下拉刷新并允许我的代码运行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接