繁体   English   中英

在UITableviewCell中使用自动布局

[英]Using auto layout in UITableviewCell

我正在尝试使用snapKit在swift中使用自动调整大小的UITableView单元! UITableView上设置的相关标志如下:

self.rowHeight = UITableViewAutomaticDimension
self.estimatedRowHeight = 70.0

我在customUITableviewCell类中定义了一个UITextField,如:

var uidTextField: UITextField = UITextField()

我自定义UITableViewCell本字段的初始设置如下所示:

self.contentView.addSubview(uidTextField)
uidTextField.attributedPlaceholder = NSAttributedString(string: "Woo Hoo", attributes: [NSForegroundColorAttributeName:UIColor.lightGrayColor()])
uidTextField.textAlignment = NSTextAlignment.Left
uidTextField.font = UIFont.systemFontOfSize(19)
uidTextField.returnKeyType = UIReturnKeyType.Done
uidTextField.autocorrectionType = UITextAutocorrectionType.No
uidTextField.delegate = self
uidTextField.addTarget(self, action: "uidFieldChanged", forControlEvents: UIControlEvents.EditingChanged)
uidTextField.snp_makeConstraints { make in 
    make.left.equalTo(self.contentView).offset(10)
    make.right.equalTo(self.contentView)
    make.top.equalTo(self.contentView).offset(10)
    make.bottom.equalTo(self.contentView).offset(10)
}

当我运行代码时,它显示为cut off并在控制台中给出一个错误:

仅警告一次:检测到约束模糊地建议tableview单元格的内容视图的高度为零的情况。 我们正在考虑无意中崩溃并使用标准高度。

我的autoLayout约束是否有问题,或者这是UIControls和UITableView单元的自动调整问题?

在SnapKit(和Masonry)中,您必须使用负值在视图的右侧或底部添加填充。 您在bottom约束上使用offset(10) ,这会导致文本字段的底部10pt被切断的效果。

要解决这个问题,你必须给你的底部约束一个负偏移量:

uidTextField.snp_makeConstraints { make in 
    make.left.equalTo(self.contentView).offset(10)
    make.right.equalTo(self.contentView)
    make.top.equalTo(self.contentView).offset(10)
    make.bottom.equalTo(self.contentView).offset(-10)
}

或者你可以通过这样做获得相同的约束:

uidTextField.snp_makeConstraints { make in 
    make.edges.equalTo(contentView).inset(UIEdgeInsetsMake(10, 10, 10, 0))
}

当您使用inset()方式时,必须使用正值和底部插入的正值。

我不明白为什么SnapKit使用底部和右侧的负值。 我认为这是违反直觉的,有点令人困惑。

编辑 :这是一个很好的例子(我用一个包含UITextField 3个自定义单元格对一个tableView进行了硬编码):

视图控制器:

import UIKit
import SnapKit

class ViewController: UIViewController {
    let tableView = UITableView()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(tableView)
        tableView.dataSource = self
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 70
        tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "CustomCell")

        tableView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view)
        }
    }
}

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

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath)
        return cell
    }
}

CustomTableViewCell

import UIKit

class CustomTableViewCell: UITableViewCell {
    let textField = UITextField()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        textField.attributedPlaceholder = NSAttributedString(string: "Woo Hoo", attributes: [NSForegroundColorAttributeName:UIColor.lightGrayColor()])
        textField.textAlignment = NSTextAlignment.Left
        textField.font = UIFont.systemFontOfSize(19)
        textField.returnKeyType = UIReturnKeyType.Done
        textField.autocorrectionType = UITextAutocorrectionType.No
        contentView.addSubview(textField)

        textField.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).inset(UIEdgeInsetsMake(10, 10, 10, 0))
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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