Swift 无法为特定设备自定义UITableViewCell
我使用的是devicekit,这意味着如果手机检测到它的类型(5s、6等),那么它将加载我概述的一组特定约束。因此,我为Xs的表视图单元格设计了约束,但在使用DeviceKit并为5s设置约束时,它不会更新约束。这是代码。此函数在视图did load中加载Swift 无法为特定设备自定义UITableViewCell,swift,uitableview,constraints,Swift,Uitableview,Constraints,我使用的是devicekit,这意味着如果手机检测到它的类型(5s、6等),那么它将加载我概述的一组特定约束。因此,我为Xs的表视图单元格设计了约束,但在使用DeviceKit并为5s设置约束时,它不会更新约束。这是代码。此函数在视图did load中加载 func deviceKitConstraints() { if device.isOneOf(smallGroup) { func tableView(_ tableView: UITableView, cellFo
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
}
}
任何帮助都将不胜感激
如果此设备的更新/约束有效,它将从屏幕上消失。您的方法位于另一个方法中,如果这是一个委托方法,它将不可见 将其放在视图控制器的全局范围内,并根据方法内部的条件设置它的单元格(如果它是
UITableViewController
,请将override
关键字放在此方法之前)
在您的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
}
}
除非这是一个非常特殊的用例(您的问题中没有列出优化/附加逻辑),否则您可能应该看看Size类()。它们将帮助您在不编写代码的情况下实现同样的目标,并在过程中帮助您将逻辑与设计分离。
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
}
}