import UIKit
class NewOrdersViewControllers: UIViewController,UITableViewDelegate,UITableViewDataSource {
var items = ["WE","Heart","Swift"]
@IBOutlet var tableView:UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
// Do any additional setup after loading the view.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected cell #\(indexPath.row)!")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
它显示我运行应用程序时的错误。我使用xib在swift中创建了table view
,但它显示以下错误:
NSArgumentException:' - [UIView tableView:numberOfRowsInSection:]: 无法识别的选择器发送到实例0x7c9a6ed0“”
答案 0 :(得分:1)
请确保您已将DataSource
和Delegate
提供给UITableView
tableView.delegate = self
tableView.dataSource = self
您在' numberOfSections`中定义numberOfRows
。用我的代码替换下面的代码:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
更改为以下内容:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
答案 1 :(得分:1)
我认为你还没有设置tableView的委托和数据源,它恰好来自xib。这是人们在使用UITableView时的常见问题。 使用swift语法为以下objective-c代码
设置tableview的委托和数据源self.tableView.delegate = self
self.tableView.dataSource = self
这些语句应该在你的viewDidLoad方法中。如果你不想在单元格上处理来自用户的点击,那么第一个不是必须的。你必须写第二个来显示信息。
告诉我是否还需要其他东西。
干杯。
答案 2 :(得分:0)
您收到的错误消息表示您已将dataSource
的{{1}}属性设置为视图,而不是tableView
已添加viewController
tableView:numberOfRowsInSection:
1}}等等方法。