2015-06-01 31 views
14

olarak adlandırılamıyor UIViewController içine bir tablo görünümü ekledim. Ama kodum çalışmıyor. Kontrol ettiğimde, tablo görüntüleme işlevlerinin hiçbirinin çağrılmadığını buldum.Swift - UIViewController içindeki UITableView, UITableView işlevleri

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

Bunun için herhangi bir çözüm var mı?

sayesinde

+1

Temsilci ve veri kaynağını ayarladınız mı? – Asaf

cevap

39

:

viewDidLoad sonuna ekleyin:

authorArticlesTableView.delegate = self 
authorArticlesTableView.dataSource = self 
13

Kümesi tablosu delegate ve dataSource:

Bir tablo görünümünde temsilci/veri kaynağı olarak görünüm denetleyicisi belirlemek zorunda
override func viewDidLoad() { 
    super.viewDidLoad() 

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

Bazen UITableViewCell'i UITableView'a dökmeyi ve bırakmayı unutun. XCode, TableView'ın Cell olduğunu anlamıyor. Varsayılan olarak, UITableView öğesini UIViewController içine döker ve bırakın. UITableView’in Cell’e sahip olduğunu görüyorum. Ama UITableViewCell'i de UITableView'a dökmeniz ve bırakmanız gerekiyor.

Bu benimle çalışır.