2017-05-30 39 views
-1

我想在應用程序中完成對視圖控制器的簡單添加,但它證明是一個挑戰。我正在試圖添加一個基本上做的列表到這個視圖控制器。我有一個數組'list',當我運行應用程序時應該顯示其項目,但是我得到空白。我已經檢查了常見的錯誤,例如沒有將表視圖連接到視圖控制器,對tableview等沒有約束。我看不出我做錯了什麼,但希望有一組不同的眼睛能指出我正確的方向。代碼如下:在Swift中使用數組的表視圖

class ThirteenthViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

var list = ["hero", "The Boys of Brighton Beach", "This Time is Different", "BS Book"] 

@IBOutlet weak var collectionTableView: UITableView! 

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return (list.count) 

} 

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell") 

    cell.textLabel?.text = list[indexPath.row] 

    return(cell) 

} 

func tableView( tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) 
{ 

    if editingStyle == UITableViewCellEditingStyle.delete 
    { 

     self.list.remove(at: indexPath.row) 
     collectionTableView.reloadData() 

    } 

} 


@IBAction func dismissCollection(_ sender: Any) { 

    dismiss(animated: true, completion: nil) 

} 


override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 


} 

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

你是否檢查過你的代碼是否被調用,如果有的話,返回什麼值? –

+0

對不起。我還很新,所以我該怎麼做?謝謝! –

+0

斷點,打印語句,類似的東西。 –

回答

2

你有這兩條線增加您的viewDidLoad功能

self.collectionTableView.dataSource = self 
self.collectionTableView.delegate = self 

龍解釋,你得告訴您取得數據的控制器,所以用那個代碼你會說dataSource來自這個控制器和委託。

希望它有幫助。

0

您需要將數據源添加到故事板中的表格視圖。 這個鏈接應該對你有所幫助Answer