Swift – UITableView里面的UIViewController,不调用UITableView函数

我在UIViewController中插入了一个tableview。 但是我的代码不起作用。 当我检查时,我发现没有任何的tableview函数没有被调用。

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

任何解决scheme?

谢谢,

您必须将您的视图控制器设置为表视图委托/数据源:

添加到viewDidLoad的末尾:

 authorArticlesTableView.delegate = self authorArticlesTableView.dataSource = self 

设置表delegatedataSource

 override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self } 

有时,如果你忘记把UITableViewCell拖放到UITableView。 XCode不理解TableView有Cell。 默认情况下,当你拖放UITableView到UIViewController。 我看到UITableView有Cell。 但是你需要将UITableViewCell拖放到UITableView中。

这是我的工作。