2017-01-01 53 views
2

因此,我一直在關注在線Swift課程,並創建一個表視圖。我幾乎從教程中複製了所有代碼,只做了一些調整(因爲課程是用老版本的swift教的,但我使用的是swift 3)。根據教程,我應該得到一個顯示名稱的4行表格視圖,但我得到的是一個「信號SIGABRT」錯誤。並沒有具體指出錯誤。連接插座「數據源」和「委託」後出現錯誤「線程1:信號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:. 
} 
} 

非常感謝您提前!

+0

如果您可以創建項目的ZIP文件並將其託管在WeTransfer或某些此類服務上,我認爲幫助您更容易。 –

+0

它現在在Wetransfer上,鏈接是https://wetransfer.com/downloads/b7321f3dadcd688228715bba053d159920170101114400/4a12c5 – Frostless

回答

0

在夫特3所述方法cellForRowAtindexPath的簽名是

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

看到其也需要在tableView(_ tableView: UITableView, numberOfRows...下劃線之後的空格字符。

而且你應該使用在Interface Builder

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 
+0

非常感謝! :) – Frostless

1

我能想到的唯一問題。那是你的tableview方法標題不匹配的數據源方法標題:

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

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

的問題在cellForRow方法。它不符合DataSource方法。

+0

感謝幫助:) – Frostless

0

我檢查你貼在WeTransfer項目,並有幾個問題,設計環保電池。

首先,正如hasan83所提到的,您的方法簽名是錯誤的。所以無法找到UITableViewDataSource的方法。這本身會導致崩潰。我建議您在Xcode中使用自動完成功能,以便自動獲取正確的方法簽名。只需輸入tableView即可獲得tableView dataSource和委託方法的列表(只要您的當前類添加了這些協議)。

此外,它似乎從頭或其他東西中複製了一個屬性@available(iOS 2.0, *),應該刪除此代碼,因爲它被視爲屬性。

固定項目爲here

+0

非常感謝您的幫助隊友!感激它1 – Frostless