原型可重用的tableview单元格渲染子视图很差

时间:2014-10-21 22:15:57

标签: ios objective-c iphone uitableview

这是我遇到这个特殊问题的第二个项目。今天早些时候,我通过故事板设置了一个tableview和prototype cell。我添加了带有标签号的子视图,因此我可以从cellforrowatindexpath委托方法中获取它们。虽然当我运行应用程序时,子视图不在正确的位置。我使用autosizing进行布局,并确保设置委托。有人有这个问题吗?

可能的相关性:有时当我离开故事板并回来时,子视图改变了它们的框架,使它们是平的(高度= 0)或x已经随机变为1500。不知道为什么,但这发生在旧项目我也有问题。旧项目我通过重置cellforrowatindexpath方法中的所有子视图的帧来解决了这个问题,但我并不喜欢这个作为合法的解决方案。

编辑:这是一些代码和截图。

//MARK: - UITableview delegate
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 93.0
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 7;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("order") as? UITableViewCell
    println("cell: \(cell)")
    var cancel = cell?.viewWithTag(4) as UIButton
     var price = cell?.viewWithTag(3) as UILabel
     var date = cell?.viewWithTag(2) as UILabel
     var address = cell?.viewWithTag(1) as UILabel
    cancel.addTarget(self, action: "cancelOrder:", forControlEvents: UIControlEvents.TouchUpInside)

    return cell!
}

screenshot

1 个答案:

答案 0 :(得分:1)

我最近遇到过这个问题。对于自定义单元格中的内容视图,似乎是自动布局的错误。

我所做的只是在awakeFromNib中,我的自定义单元格重置自动调整遮罩,一切都按预期开始工作。

- (void) awakeFromNib
{
    self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
相关问题