2015-11-03 79 views
0

我有一個UITableView通過遠程JSON文件填充問題。我有兩個功能:Swift tableView ambigious

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return newsdata.count 
} 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! NewsTableViewCell 
    let nachricht = newsdata[indexPath.row] 
    cell.NewsHeadline.text = nachricht.newstitel 
    return cell 
} 

設置和運行,並與連接到另一個視圖控制器與UIWebView ID爲「ShowNewsDetail」 Segue公司。然而,連接兩個控制器中的代碼不工作:

override func prepareForSegue(segue: UIStoryboardSegue, 
     sender: AnyObject?) { 

     if segue.identifier == "ShowNewsDetails" { 
      let detailViewController = segue.destinationViewController 
       as! NewsDetailViewController 
      let myIndexPath = self.tableView.indexPathForSelectedRow() 
      let row = myIndexPath?.row 
      detailViewController.webSite = newsdata[row!] 
     } 
} 

的Xcode 7.1報告「不明確的參照構件‘的tableView’」和點之前這兩個函數爲「候選」。我不知道如何挑選候選人或解決這個令人討厭的錯誤。

+0

什麼是兩個函數在Xcode指向的錯誤之前? 'numberOfRowsInSection'「&」'cellForRowAtIndexPath'「函數? –

+0

你初始化了什麼類?它是一個VIewController還是tableView控制器?如果是這樣的話,請參閱此鏈接:http://stackoverflow.com/questions/30028129/how-to -fix-nib-but-didnt-get-a-uitableview-error/33484728?noredirect = 1#comment54770542_33484728。我猜你沒有初始化tableView。 – lukaivicev

+0

lukeslvi答案導致我的錯誤。填滿表格,從表格視圖到控制器的出口缺失 – Matt

回答

0

我檢查了你的代碼,一切正常。但在我的情況下,Swift應用了一些變化。我的xCode也是7.1版本。我只是在細節控制器中傳遞一個字符串。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

     if segue.identifier == "ShowNewsDetails" { 
      let detailViewController = segue.destinationViewController 
      as! NewsDetailViewController 
      let myIndexPath = self.tableView.indexPathForSelectedRow! 
      let index = myIndexPath.row 
      detailViewController.tempStr = String(index) 
     } 
    } 


乾杯。

0

我的猜測是你有一個名爲tableView的插座。編譯器將插座的符號看作與兩個方法名稱的符號相同。

因此,不是這樣的:

@IBOutlet weak var tableView: UITableView! 

您的出口重命名爲別的東西:

@IBOutlet weak var newsTableView: UITableView! 

然後使用這個插座變量您prepareForSegue代碼:

let myIndexPath = self.newsTableView.indexPathForSelectedRow! 
+0

感謝您的答案 - 如果可以的話,我會加註他們。lukeslvi帶我回答 - 表格視圖沒有正確初始化。 – Matt