连接出口“datasource”和“delegate”后得到错误“Thread 1:signal SIGABRT”

时间:2017-01-01 10:03:53

标签: ios swift

所以我一直关注在线Swift课程,我正在创建一个表视图。我几乎复制了教程中的所有代码,只进行了一些调整(因为课程是用较旧版本的swift教授的,但我使用的是swift 3)。根据教程我应该得到一个显示名称的4行表视图,但我得到的是“信号SIGABRT”错误。并没有特别指出错误。

这里有什么问题?如果我没有连接“数据源”插座,我可以运行模拟器没有问题,但我不会显示名称。但是,如果我进行连接,我甚至无法运行模拟器。我真的希望我的问题清晰,读者友好!

import UIKit

class ViewController: UIViewController, UITableViewDelegate{

var cellContent = ["xiaohong","xiaohua", "xiaogang" ,"xiaoxiao"]

public override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}
public  func tableView(_tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return cellContent.count
}
public  func tableView(_tableView: UITableView, cellForRowAtindexPath indexPath:NSIndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: UITableViewCellStyle.default,reuseIdentifier:"Cell")
    cell.textLabel?.text = cellContent[indexPath.row]
    return cell    }
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

为了更清楚,我想在这里粘贴整个错误信息!

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    return true
}

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}

非常感谢你!

3 个答案:

答案 0 :(得分:1)

我能想到的唯一问题。您的tableview方法标头是否与数据源方法标头不匹配:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    <#code#>
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    <#code#>
}

cellForRow方法中的问题。它与DataSource方法不匹配。

答案 1 :(得分:0)

在Swift 3中,方法cellForRowAtindexPath的签名是

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

查看下划线后面的空格字符,这也是tableView(_ tableView: UITableView, numberOfRows...中的空格字符。

此外,您应该使用Interface Builder

中设计的可重用单元格
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

答案 2 :(得分:0)

我检查了你在WeTransfer上发布的项目,并且有几个问题。

首先,正如hasan83所提到的,你的方法签名是错误的。因此无法找到UITableViewDataSource的方法。这会导致本身崩溃。我建议您使用Xcode中的自动完成功能,以便自动获取正确的方法签名。只需开始键入tableView以获取tableView dataSource和委托方法的列表(前提是您当前的类添加了这些协议)。

此外,您似乎从标题或其他内容复制了属性@available(iOS 2.0, *),此代码应被删除,因为它被视为属性。

固定项目为here