2017-02-12 65 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") 
    } 
} 

我沒有經歷過的廈門國際銀行嘗試過,但是我覺得很有道理