2016-08-16 107 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   
}