ios - Swift - UIViewController 内的 UITableView,不调用 UITableView 函数

标签 ios uitableview swift

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

有什么解决办法吗?

谢谢,

最佳答案

您必须将您的 View Controller 设置为 TableView 委托(delegate)/数据源:

添加到 viewDidLoad 的末尾:

authorArticlesTableView.delegate = self
authorArticlesTableView.dataSource = self

关于ios - Swift - UIViewController 内的 UITableView,不调用 UITableView 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30576291/

相关文章:

iphone - 表格 View 单元格随文本大小展开或收缩

ios - UICollectionView AutoSizingCells 在 reloadData() 上出现奇怪的跳跃/故障

ios - 在 NSManaged 变量上使用属性观察器

ios - 将 cocoapods 添加到成熟的 react native 项目后,我的应用程序存档为单独的应用程序,而不是对现有应用程序的更新

ios - dyld : Library not loaded: @rpath/XCTest. 框架/XCTest

ios - 按下 SFSafariViewController 时出现黑色标签栏

objective-c - UITableView 滚动后 UIButton 不可点击

ios - 将表的行数设置为特定实体

android - Firebase IOS UTM 安装跟踪

ios - Swift:仅在开头删除字符串的特定字符