2016-07-05 56 views
0

我现在有,设置了自定义数据源在其init函数一个UITableViewController:的UITableViewController与自定义数据源没有显示细胞

class BookmarkTableViewController: UITableViewController { 
    var date: Date 

    // MARK: - Init 
    init(date: Date, fetchAtLoad: Bool) { 
    self.date = date 
    super.init(style: .plain) 
    self.tableView.dataSource = BookmarkDataSource(date: date) 
    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") 
    } 

// ... 
} 

自定义数据源如下:

class BookmarkDataSource: NSObject { 
    let date: Date 

    init(date: Date) { 
    self.date = date 
    super.init() 
    } 
} 

// MARK: - UITableViewDataSource 
extension BookmarkDataSource: UITableViewDataSource { 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 3 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = UITableViewCell() 
    cell.textLabel?.text = "Test content" 

    return cell 
    } 
} 

但是,当我在模拟器上或设备上运行时,表格视图中什么也没有显示。有谁知道我错过了什么?

注:我使用的Xcode 8.0 Beta版和雨燕3.

回答

5

您需要存储的强引用您的BookmarkDataSource对象替换

let cell = UITableViewCell() 

。 tableView的dataSource在你发布的代码中为零。

class BookmarkTableViewController: UITableViewController { 
    var date: Date 
    var dataSource:BookmarkDataSource 

    // MARK: - Init 
    init(date: Date, fetchAtLoad: Bool) { 
     self.date = date 
     super.init(style: .plain) 
     dataSource = BookmarkDataSource(date: date) 
     self.tableView.dataSource = dataSource 
     self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") 
    } 

    // ... 
} 
+0

是的!就是这个。谢谢! – Aliou

+1

我弄糟了dataSource变量的创建,但你明白了! – bradkratky

0

我觉得你的细胞没有被正确实例。

尝试

let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)