2015-06-01 9 views
14

नहीं कहा गया है मैंने UIViewController के अंदर एक टेबलव्यू डाला। लेकिन मेरा कोड काम नहीं कर रहा है। जब मैंने चेक किया तो मैंने पाया कि टेबलव्यू फ़ंक्शंस में से कोई भी नहीं कहा जाता है।स्विफ्ट - UIViewController के अंदर UITableView, UITableView फ़ंक्शंस को

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

इसके लिए कोई समाधान?

धन्यवाद,

+1

क्या आपने प्रतिनिधि और डेटा स्रोत सेट किया था? – Asaf

उत्तर

39

आप एक तालिका दृश्य प्रतिनिधि/डेटा स्रोत के रूप में अपने दृश्य नियंत्रक सेट करने के लिए:

viewDidLoad के अंत में जोड़ने:

authorArticlesTableView.delegate = self 
authorArticlesTableView.dataSource = self 
13

सेट तालिका delegate और dataSource:

override func viewDidLoad() { 
    super.viewDidLoad() 

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

कभी-कभी, यदि यो आप UITableViewCell को UITableView में लपेटने और छोड़ना भूल जाते हैं। XCode समझ में नहीं आता TableView सेल है। डिफॉल्ट रूप से, जब आप UIViewController में UITableView को छोड़कर छोड़ देते हैं। मुझे लगता है कि UITableView में सेल है। लेकिन आपको UITableViewCell को UITableView में भी लपेटने और छोड़ने की आवश्यकता है।

यह मेरे साथ काम करता है।