Ios ViewController不确认协议UITableViewDataSource
我做得很好,但仍然显示错误“ViewController不符合协议” 我在这个网站上搜索了很多,我发现了同样的问题,但并没有找到任何正确的答案 我已经将UITableView连接添加到UITableViewDelegate和UITableViewDataSource,但仍然显示错误我应该怎么做 这是我的密码Ios ViewController不确认协议UITableViewDataSource,ios,uitableview,swift,Ios,Uitableview,Swift,我做得很好,但仍然显示错误“ViewController不符合协议” 我在这个网站上搜索了很多,我发现了同样的问题,但并没有找到任何正确的答案 我已经将UITableView连接添加到UITableViewDelegate和UITableViewDataSource,但仍然显示错误我应该怎么做 这是我的密码 class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { override fu
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet var tableView: UITableView!
func tableView(tableView:UITableView!, numberOfRowsInSection section:Int) -> Int
{
return 20
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
cell.textLabel.text="row#\(indexPath.row)"
cell.detailTextLabel.text="subtitle#\(indexPath.row)"
return cell
}
}
请注意删除了
此处错误的委托方法中的代码>
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet var tableView: UITableView!
func tableView(tableView:UITableView, numberOfRowsInSection section:Int) -> Int
{
return 20
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "mycell")
cell.textLabel.text="row#\(indexPath.row)"
cell.detailTextLabel.text="subtitle#\(indexPath.row)"
return cell
}
}
cellForRowAtIndexPath方法上的代码存在其他错误
UITableViewCell.textLabel和UITableViewCell.detailTextLabel返回可选的UILabel,因此您需要首先将它们展开(表示在选项之后添加!)。因此,改变这两行如下,你是好的
cell.textLabel!.text="row#\(indexPath.row)"
cell.detailTextLabel!.text="subtitle#\(indexPath.row)"
此外,CellForRowatineXpath的方法签名是错误的。拆下!在最后一个UITableViewCell上。应该是这样的:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
好的,我从代码中删除了这两行,但仍然显示相同的错误。你确定你捕获了这四行吗代码>?当我编译代码时,我发布了错误消息更改。