调整UITableView单元格上的SubView约束

时间:2016-01-14 02:05:11

标签: ios swift uitableview constraints subview

我有一个带2节的UITableView。第一部分( VoucherCell )将填充数据库中的数据。第二部分( KodeVoucherCell )将填充用户使用简单表格输入的数据。我将这个简单的表单创建为SubView(见下图)。

enter image description here

然后我设置第二部分使用以下代码加载SubView:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = UITableViewCell.init()

    if(indexPath == NSIndexPath(forRow: 0, inSection: 1)) {
        cell = tableView.dequeueReusableCellWithIdentifier("KodeVoucherCell")!
        cell.addSubview(inputKodeVoucherSubView)
    } else {
        cell = tableView.dequeueReusableCellWithIdentifier("VoucherCell")!

        cell.indentationLevel = indexPath.length - 1
        cell.accessoryType = .DisclosureIndicator
        cell.textLabel!.text = "Voucher A"
    }

    return cell
}

现在问题是,虽然我已经设置了SubView的约束,但是当我运行应用程序时,它看起来像这样:

enter image description here

如何解决这个问题?我试图修改约束但仍然看起来相同。感谢。

1 个答案:

答案 0 :(得分:1)

您不应该直接将子视图添加到UITableViewCell。将它们添加到单元格的contentView。替换:

cell.addSubview(inputKodeVoucherSubView)

使用:

cell.contentView.addSubview(inputKodeVoucherSubView)