简体   繁体   中英

Adjust SubView constraints on a UITableView Cell

I have an UITableView with 2 section. The first section ( VoucherCell ) will be filled with data from a database. The second section ( KodeVoucherCell ) will be filled with data inputted by the user using a simple form. I create that simple form as a SubView (see picture below).

在此处输入图片说明

Then I set the second section to load the SubView using the code below :

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
}

Now the problem is, although I have set the constraints of the SubView, while I run the app, it looks like this :

在此处输入图片说明

How to fix this? I tried to modify the constraint but still get the same look. Thanks.

You shouldn't be adding subviews directly to a UITableViewCell. Add them to the cell's contentView . Replace:

cell.addSubview(inputKodeVoucherSubView)

with:

cell.contentView.addSubview(inputKodeVoucherSubView)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM