无法为特定设备自定义UITableViewCell

时间:2019-01-07 14:15:23

标签: swift uitableview constraints

enter image description here

我正在使用devicekit,这意味着如果手机检测到其类型(5s,6等...),则它将加载我概述的一组特定约束。因此,我为Xs的表视图单元格设计了约束,但是在使用DeviceKit并将其设置为5s时,它不会更新约束。这是代码。此功能已在视图加载后加载。

func deviceKitConstraints() {
    if device.isOneOf(smallGroup) {
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell
            cell.regoAnswer.center = CGPoint(x: self.view.center.x, y: self.view.center.y)
            return cell
        }
    }

任何帮助将不胜感激

如果该设备的更新/约束无法正常工作,它应该会退出屏幕。

2 个答案:

答案 0 :(得分:1)

您的方法在另一个方法内部,如果应该作为委托方法,这将使其不可见。

将其放在视图控制器的全局范围内,并根据方法内部的条件将其设置为单元格(如果为UITableViewController,请在此方法前放置override关键字)

class ViewController: UIViewController {

    ...

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if device.isOneOf(smallGroup) {
            ... // small
        } else {
            ... // large
        }
    }

}

答案 1 :(得分:0)

在您的ViewController中或声明tableView的位置:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if device.isOneOf(smallGroup) {
            let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! SmallCustomCell

            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! LargeCustomCell

            return cell
        }
    }

然后在单独的类中定义SmallCustomCell和LargeCustomCell,例如:

class SmallCustomCell: UITableViewCell {
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setup()
    }

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

    fileprivate func setup() {
        // layout views + constraints
    }
}