Tableview简单的显示值

我是一个swift3的stduent和非常初学者。 在这里,我只想在tableview中显示“First”,“Second”,“Third”,看起来不太好。

来自YouTube的教程是swift2,我试图转换成swift3。 我认为这个问题与代码有关:

let Cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell 

我努力search像https://www.codeschool.com/blog/2016/09/14/evolving-to-swift-3/,但我不知道发生了什么。 Xcode无法检测到代码的任何错误,它可以运行。 在iPhone模拟器的桌面视图是空的或整个屏幕是黑色的。

我使用导航控制器来实现它。 表视图单元格中viewcontoller和iderntifer中的类在根视图控制器中设置良好。 请帮帮我。 谢谢。

 import Foundation import UIKit class ViewController: UITableViewController { var Array = [String]() override func viewDidLoad() { super.viewDidLoad() Array = ["First","Second","Third"] } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return Array.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let Cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell Cell.textLabel?.text = Array[indexPath.row] return Cell } 

}

在这里输入图像说明

结果在控制台中:

 2016-10-24 01:10:38.111399 TableApp[27276:2609723] subsystem: com.apple.UIKit, category: HIDEventFiltered, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0 2016-10-24 01:10:38.114012 TableApp[27276:2609723] subsystem: com.apple.UIKit, category: HIDEventIncoming, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0 2016-10-24 01:10:38.122822 TableApp[27276:2609719] subsystem: com.apple.BaseBoard, category: MachPort, enable_level: 1, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 0, privacy_setting: 0, enable_private_data: 0 2016-10-24 01:10:38.138706 TableApp[27276:2609660] subsystem: com.apple.UIKit, category: StatusBar, enable_level: 0, persist_level: 0, default_ttl: 0, info_ttl: 0, debug_ttl: 0, generate_symptoms: 0, enable_oversize: 1, privacy_setting: 2, enable_private_data: 0 2016-10-24 01:10:38.200 TableApp[27276:2609660] *** Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3599.6/UITableView.m:8035 2016-10-24 01:10:38.203 TableApp[27276:2609660] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (<UITableView: 0x7fbac906bc00; frame = (0 0; 375 667); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x61000004e400>; layer = <CALayer: 0x610000235ce0>; contentOffset: {0, -64}; contentSize: {375, 132}>) failed to obtain a cell from its dataSource (<TableApp.GetStringViewController: 0x7fbac8d06180>)' *** First throw call stack: ( 0 CoreFoundation 0x000000010fab034b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010c9dc21e objc_exception_throw + 48 2 CoreFoundation 0x000000010fab4442 +[NSException raise:format:arguments:] + 98 3 Foundation 0x000000010c572edd -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195 4 UIKit 0x000000010d429ff2 -[UITableView _configureCellForDisplay:forIndexPath:] + 222 5 UIKit 0x000000010d435c10 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 860 6 UIKit 0x000000010d435e07 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74 7 UIKit 0x000000010d409871 -[UITableView _updateVisibleCellsNow:isRecursive:] + 3295 8 UIKit 0x000000010d43f189 -[UITableView _performWithCachedTraitCollection:] + 110 9 UIKit 0x000000010d4259e3 -[UITableView layoutSubviews] + 222 10 UIKit 0x000000010d38d344 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237 11 QuartzCore 0x0000000112875cdc -[CALayer layoutSublayers] + 146 12 QuartzCore 0x00000001128697a0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 13 QuartzCore 0x000000011286961e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 14 QuartzCore 0x00000001127f762c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280 15 QuartzCore 0x0000000112824713 _ZN2CA11Transaction6commitEv + 475 16 QuartzCore 0x0000000112825083 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113 17 CoreFoundation 0x000000010fa54e17 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 18 CoreFoundation 0x000000010fa54d87 __CFRunLoopDoObservers + 391 19 CoreFoundation 0x000000010fa394b6 CFRunLoopRunSpecific + 454 20 UIKit 0x000000010d2c2db6 -[UIApplication _run] + 434 21 UIKit 0x000000010d2c8f34 UIApplicationMain + 159 22 TableApp 0x000000010c3fb5bf main + 111 23 libdyld.dylib 0x00000001109da68d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) 

试试这个代码:在Swift 3中testing代码

注意:正如你刚才提到的,你刚刚开始编写Swift 3.我以编程方式创build了一个tableView。 只是,复制下面的代码,并粘贴到您的viewController.run项目…

  import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { private let MyArray: NSArray = ["First","Second","Third"] private var myTableView: UITableView! override func viewDidLoad() { super.viewDidLoad() // Status Bar let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height // View let displayWidth: CGFloat = self.view.frame.width let displayHeight: CGFloat = self.view.frame.height // TableView myTableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight)) // Cell myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell") // DataSource myTableView.dataSource = self // Delegate myTableView.delegate = self // View self.view.addSubview(myTableView) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("Num: \(indexPath.row)") print("Value: \(MyArray[indexPath.row])") } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return MyArray.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath) cell.textLabel!.text = "\(MyArray[indexPath.row])" return cell } } 

输出: 在这里输入图像说明