2014-10-08 50 views
0

我做得很好,但仍显示错误“ViewController不确认协议” 我在这个网站上搜索很多我找到相同的问题,但没有找到任何正确的答案 我已经添加连接向的UITableView和的UITableViewDelegate但UITableViewDataSource仍然显示我应该怎么办ViewController不确认协议UITableViewDataSource

错误的,这里是我的代码

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 

} 



} 

回答

1

注意,在委托方法这是错在这里去除!的。

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 

} 



} 
+0

删除此“!”后,仍然显示相同的错误 – suthar 2014-10-08 11:03:56

+0

你确定你抓住了所有四个'!'?当我编译代码时,我发布了错误消息更改。 – fluidsonic 2014-10-08 11:26:13

+0

解决了谢谢 – suthar 2014-10-08 11:43:23

0

您对cellForRowAtIndexPath方法的代码有其他错误。

UITableViewCell.textLabel和UITableViewCell.detailTextLabel返回可选的UILabel,因此您需要首先解开它们(意思是在选项之后添加!)。所以改变下面的2行,你很好。

cell.textLabel!.text="row#\(indexPath.row)" 
cell.detailTextLabel!.text="subtitle#\(indexPath.row)" 

而且,对于方法的cellForRowAtIndexPath签名是错误的。去除 !在最后一个UITableViewCell上。应该是这样的:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
+0

okey我从我的代码中删除了这两行,但仍然显示相同的错误 – suthar 2014-10-08 11:12:53

+0

你是对的,还有另一个错误。更新的答案。 – dadalar 2014-10-08 11:18:22

相关问题