【问题标题】:icon changes the position on selection of UITableViewCell图标更改 UITableViewCell 选择时的位置
【发布时间】:2017-02-12 13:18:07
【问题描述】:

我有一个原型单元。第一次打开表视图控制器时。单元格如下所示:

问题 #1:我有一个限制,即日历图标的 leading space to superview 应该是 10 点。但实际上是 16 分。

然后很奇怪的事情发生了。我点击单元格。它进入下一个屏幕。然后我回到这个屏幕。并且图标位置正确(10points space):

接下来发生的事情是:如果我单击单元格,在很短的时间内(突出显示时)图标向右移动 6 点:

问题 #2:如何避免这种“移动”?

我尝试做的:将这两行添加到 didSelectRowAtIndexPath 和 willSelectRowAtIndexPath cell?.setNeedsLayout() cell?.setNeedsUpdateConstraints()

但这并没有多大帮助。如果您需要更多详细信息,请告诉我。顺便说一句,我没有改变任何约束的代码。一切都是静态的。

编辑:这是图标(imageView)的约束的样子:

注意:leading space to superview 等于 2,因为它有一些默认值 8(所以 2+8=10)。

【问题讨论】:

  • Fam 你有没有对你的单元格添加所有这些标签?
  • @TusharSharma 我不确定我是否理解你的问题。虽然我认为我的答案是“不”:)
  • 如果你在这里发布一些有用的代码会更有帮助。
  • @aircraft 谢谢!你想看什么代码?请告诉我。在 didSelectRow 我只是做'执行segue' ..
  • @Tung Fam,你如何创建单元格?使用 xib 还是什么?

标签: ios xcode autolayout constraints tableview


【解决方案1】:

请调整并尝试我的代码。在注册此单元之前

tableView.register(UserCell.self, forCellReuseIdentifier: "myCell")

并初始化

let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! UserCell

======

class UserCell: UITableViewCell {

override func layoutSubviews() {
    super.layoutSubviews()

    textLabel?.frame = CGRect(x: 56, y: (textLabel?.frame.origin.y)! - 2, width: (textLabel?.frame.width)!, height: (textLabel?.frame.height)!)
    detailTextLabel?.frame = CGRect(x: 56, y: (detailTextLabel?.frame.origin.y)! + 2, width: (detailTextLabel?.frame.width)!, height: (detailTextLabel?.frame.height)!)

}
let profileImagView: UIImageView = {
    let imageView = UIImageView()
    imageView.translatesAutoresizingMaskIntoConstraints = false


    imageView.layer.cornerRadius = 20
    imageView.layer.masksToBounds = true
    imageView.image = UIImage(named: "logo")
    imageView.contentMode = .scaleAspectFill
    return imageView
}()



override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
    addSubview(profileImagView)
    profileImagView.leftAnchor.constraint(equalTo: self.leftAnchor,constant:8).isActive  = true
    profileImagView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
    profileImagView.widthAnchor.constraint(equalToConstant: 40).isActive = true
    profileImagView.heightAnchor.constraint(equalToConstant: 40).isActive = true
}

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

我没有通过 xib 尝试过,但我认为它是有道理的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 2016-12-12
    • 2010-12-23
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    相关资源
    最近更新 更多