2016-10-04 63 views
0

我编程创建我的表视图有以下:的UITableView

lazy var myTableView: UITableView = 
{ 
    let tableView = UITableView() 
    tableView.delegate = self 
    tableView.dataSource = self 
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell") 
    return tableView 
}() 

我那么它作为一个子视图添加到我的观点在viewDidLoad中:

view.addSubview(myTableView) 

然后在我的方法在这里我设置了所有的UI约束条件,我用以下内容进行布置:

myTableView.widthAnchor.constraintEqualToAnchor(view.widthAnchor).active = true 
myTableView.topAnchor.constraintEqualToAnchor(temporaryContainerView.bottomAnchor).active = true 

最后,这里是我的委托/数据源的方法:

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 5 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell! 
    cell.textLabel?.text = "Test" 

    return cell 
} 

当我把上述方法断点,它好像是numberOfRowsinSection被称为很多次,但的cellForRowAtIndexPath不会被调用的。

为什么不出现?

+0

它种听起来像你的tableview是认识到,没有一个细胞实际上是在屏幕上,它(如果真)会解释为什么cellForRow没有被调用。我只看到宽度和底部的约束 - 顶/高属性怎么样?该框架默认为0,0,0,0,所以我怀疑如果你没有明确设置高度或顶部和底部约束,它的高度为0.你可以通过在你的numberOfRows委托方法中放置一个断点来检查这一点,当它被冻结时,打印出tableview的框架(p myTableView.frame) – creeperspeak

回答

0

我不得不设置centerXAnchor约束我的TableView它与translatesAutoresizingMaskIntoConstraints正确一起位于=假