2017-02-12 6 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()

Но это не очень помогает .. Пожалуйста, дайте мне знать, если вам нужно больше информации. Кстати, у меня нет кода, который меняет любые ограничения. Все статично.

EDIT: это как ограничения иконкой (ImageView) выглядит следующим образом:

enter image description here

примечание: leading space to superview равно 2, потому что у него есть значение по умолчанию 8 (так 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") 
    } 
} 

Я не пробовал через XIb, однако я думаю, что это имеет смысл