2015-06-01 16 views
14

Tôi chèn một tableview bên trong một UIViewController. Nhưng mã của tôi không hoạt động. Khi tôi kiểm tra, tôi thấy rằng không có chức năng tableview nào không được gọi.Swift - UITableView bên trong UIViewController, chức năng UITableView không được gọi là

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

Bất kỳ giải pháp nào cho điều này?

Cảm ơn,

+1

Bạn đã đặt đại biểu và nguồn dữ liệu chưa? – Asaf

Trả lời

39

Bạn phải thiết lập bộ điều khiển xem của bạn khi một cái nhìn bảng đại biểu/nguồn dữ liệu:

thêm vào phần cuối của viewDidLoad:

authorArticlesTableView.delegate = self 
authorArticlesTableView.dataSource = self 
13

Set bảng delegatedataSource:

override func viewDidLoad() { 
    super.viewDidLoad() 

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

Đôi khi, nếu yo u quên drap và thả UITableViewCell để UITableView. XCode không hiểu TableView có Cell. Theo mặc định, khi bạn thả và thả UITableView vào UIViewController. Tôi thấy UITableView có Cell. Nhưng bạn cần phải vứt và thả UITableViewCell vào UITableView.

Nó hoạt động với tôi.

Các vấn đề liên quan