2016-11-08 24 views
0

僅使用Table Views,我向第一個單元格添加了一個標籤,我試圖將其拖到視圖控制器以實現廣告代碼,但它不起作用。 (編程上還很新穎)iOS Table Views

這就是現在在視圖控制器中的全部內容。

import UIKit 

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 


    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. 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 3 
    } 

    func tableView(_tableView:UITableView, cellForRowAtIndexPath index, Path: NSIndexPath) -> UITableViewCell{ 
     return 
    } 
} 

也因爲這個,我得到「參數需要一個明確的類型錯誤

func tableView(_tableView:UITableView, cellForRowAtIndexPath index, Path: NSIndexPath) -> UITableViewCell { 
    return 
} 
+0

你給視圖控制器分配了一個類? – hussain

+2

您不會返回任何需要返回「UITableViewCell」的方法。你應該去找一個關於如何在iOS中使用'UITableView'的教程。它會幫助你...很多。 – Fogmeister

+0

你能推薦一個視頻嗎?我正在關注如何做到這一點的視頻,直到我無法添加標籤,我被卡住了 – Sas

回答

1

提供A級的視圖控制器,你必須返回一個UITableViewCell。一旦提供課程,選擇標籤並將其拖到文件中。

0

在第一部分,一定要分配類的故事板視圖控制器,否則,Xcode中沒有按「T允許你通過拖動標籤來創建@IBOutlets。

第二個,該功能預計將返回的UITableViewCell,你也不會返回任何東西。另外,如果你使用的斯威夫特3,這是新的系統ntax。要快速解決您檢查將是以下幾點:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    return UITableViewCell() 
} 
0

你必須創建一個類類型的UITableViewCell

class sampleCell: UITableViewCell { 
    @IBOutlet weak var _myLabel: UILabel! // you have to drag from story board. 
    } 

// Storybord

選擇您把標籤的單元格。

點擊單元格 - >選擇身份驗證器 - >自定義類 - >設置自定義類名稱。 // 「sampleCell」

選擇下一個標籤屬性檢查器 - >標識設置的名稱爲 「sampleCell」 //你可以把任何名稱

//視圖控制器

集出口的tableview。

 @IBOutlet weak var _tableView: UITableView! // drag from viewcontroller 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = _tableView.dequeueReusableCellWithIdentifier("sampleCell", forIndexPath: indexPath) as! sampleCell 
    cell._myLabel.text = "sample" 
    return cell 
    }