2015-06-01 19 views
14

Wprowadziłem tableview wewnątrz UIViewController. Ale mój kod nie działa. Kiedy sprawdziłem, stwierdziłem, że żadna z funkcji tableview nie jest wywoływana.Swift - UITableView wewnątrz UIViewController, funkcje UITableView nie są nazywane

class CustomViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

     @IBOutlet weak var authorArticlesTableView: UITableView! 

     var authorid: Int! 
     var authorname: String! 
     var articles: [JSON]? = [] 

     func loadArticles(){ 
      let url = "http:here.com/" + String(authorid) + "/" 
      println(url) 
      Alamofire.request(.GET, url).responseJSON { (Request, response, json, error) -> Void in 
       if (json != nil){ 
        var jsonObj = JSON(json!) 
        if let data = jsonObj["articles"].arrayValue as [JSON]?{ 
         self.articles = data 
         self.authorArticlesTableView.reloadData() 
        } 
       } 
      } 
     } 
     override func viewDidLoad() { 
      super.viewDidLoad() 
      self.loadArticles() 
      println("viewDidLoad") 
     } 

     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      println("numberOfRowsInSection") 
      return self.articles?.count ?? 0 
     } 

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
      var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomTableViewCell 
      cell.articles = self.articles?[indexPath.row] 
      println("cellForRowAtIndexPath") 
      return cell 
     } 


     func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
      performSegueWithIdentifier("WebSegue", sender: indexPath) 
      tableView.deselectRowAtIndexPath(indexPath, animated: true) 
     } 

Jakieś rozwiązanie tego?

Dzięki,

+1

Czy ustawiono delegata i źródło danych? – Asaf

Odpowiedz

39

Musisz ustawić kontroler widok jak widok tabeli delegata/źródło danych:

dodać do końca viewDidLoad:

authorArticlesTableView.delegate = self 
authorArticlesTableView.dataSource = self 
13

nakryty stół delegate i dataSource:

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.delegate = self 
    tableView.dataSource = self 
} 
0

Czasami, jeśli yo zapomnij upuścić i upuścić UITableViewCell do UITableView. XCode nie rozumie, że TableView ma Cell. Domyślnie po upuszczeniu i upuszczeniu UITableView w UIViewController. Widzę UITableView ma Cell. Ale musisz również zgrać i upuścić UITableViewCell w UITableView również.

To działa ze mną.

Powiązane problemy