2015-06-01 1 views
14

Ho inserito un tableview all'interno di un UIViewController. Ma il mio codice non funziona. Quando ho controllato ho scoperto che nessuna delle funzioni di tableview non viene chiamata.Swift - UITableView all'interno di UIViewController, le funzioni di UITableView non sono chiamate

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

Qualsiasi soluzione per questo?

Grazie,

+1

Hai impostato il delegato e l'origine dati? – Asaf

risposta

39

È necessario impostare il controller della vista come vista tabella delegato/origine dati:

aggiungere alla fine del viewDidLoad:

authorArticlesTableView.delegate = self 
authorArticlesTableView.dataSource = self 
13

Set tavolo delegate e dataSource:

override func viewDidLoad() { 
    super.viewDidLoad() 

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

A volte, se siete Ti dimentichi di caricare UITableViewCell su UITableView. XCode non capisce TableView ha Cell. Per impostazione predefinita, quando drap e rilascia UITableView in UIViewController. Vedo UITableView ha Cell. Ma devi anche rendere UITableViewCell in UITableView.

È lavoro con me.