2017-02-12 63 views
0

我有一个原型单元格。当表格视图控制器第一次打开时。细胞看起来是这样的:图标更改UITableViewCell的选择位置

enter image description here

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

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

enter image description here

那么接下来发生的事情是:如果我上单击单元格,在很短的瞬间(高亮时)的图标移动到6的权利要点:

enter image description here

问题2:如何避免这种 '运动'?

我试图做的:添加了这些2线didSelectRowAtIndexPath方法和willSelectRowAtIndexPath cell?.setNeedsLayout() cell?.setNeedsUpdateConstraints()

但它没有太大的帮助。请让我知道,如果你需要更多的细节。顺便说一句,我没有任何改变任何约束的代码。一切都是静态的。

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

enter image description here

注:leading space to superview等于2因为它有一些默认的8(SO 2 + 8 = 10) 。

+0

Fam你有没有在你的单元格上添加所有这些标签? –

+0

@TusharSharma我不知道我是否理解你的问题。虽然我认为我的回答是'不') –

+0

如果你在这里发布一些有用的代码,它会帮助更多。 – aircraft

回答

0

请改编并尝试我的代码。之前注册这个细胞

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") 
    } 
} 

我没有经历过的厦门国际银行尝试过,但是我觉得很有道理