2016-08-16 115 views
1

我已经搜索并得到了这些问题,解决了同样的问题。'ViewController'不符合协议'UITableViewDataSource'swift

但是,我不是做在回答这些提到的任何错误。

这里是我的代码:

class RightViewController: ParentViewController, UITableViewDelegate, UITableViewDataSource { 

//properties 
var itemsArray: [String] = ["set", "git"] 
@IBOutlet var tableView:UITableView! = UITableView() 


override func viewDidLoad() { 
    super.viewDidLoad() 

    //register cell 

    self.tableView!.registerClass(MenuCell.self, forCellReuseIdentifier: "MenuCell") 
    tableView!.rowHeight = UITableViewAutomaticDimension 
    tableView!.estimatedRowHeight = 140 
    tableView!.delegate = self 
    tableView!.dataSource = self 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: <TableViewDataSource> 

func numberOfSectionsInTableView(tableView: UITableView) -> Int 
{ 
    return 1 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return itemsArray.count 
} 

func tableView(tableView: UITableView, cellForRowAt indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("MenuCell", forIndexPath: indexPath) as! MenuCell 

    if (indexPath as NSIndexPath).row < itemsArray.count { 
     let option = itemsArray[(indexPath as NSIndexPath).row] 

     cell.titleLabel?.text = option 

    } 

    return cell 

} 

我已经实现了所有必需的协议方法,他们是在我的类的范围。我仍然得到这个恼人的错误。 ParentViewController在我的代码实际上是另一个UIViewController

enter image description here 在哪里我可能是错的。提前致谢。

+1

您应该遵循委托方法签名。 cellForRowAt用cellForRowAtIndexPath替换 –

回答

3

问题是,您正在使用的版本较低的swift而不是swift 3.0,方法cellForRowAt indexPath与swift 3.0一起使用,因此您需要使用此cellForRowAtIndexPath而不是此。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("MenuCell", forIndexPath: indexPath) as! MenuCell   
    if (indexPath as NSIndexPath).row < itemsArray.count { 
     let option = itemsArray[(indexPath as NSIndexPath).row]    
     cell.titleLabel?.text = option    
    }   
    return cell   
} 
相关问题