2017-09-25 95 views
0

我已经通过编程完成了UITableViewCell,并且它在iOS 10中工作得很好。但是在使用iOS 11和XCode 9进行更新后,其行为会有所不同。布局看起来像下面一样乱七八糟。UITableViewCell在iOS中的行为不同11

enter image description here

但是,如果我在电池接头然后重新排列和下面看起来很好。

enter image description here

这里UITableViewCell

import UIKit 
import SnapKit 

class AboutCell: UITableViewCell { 

    let roleLabel : UILabel = { 
     var tablelabel = UILabel() 
     tablelabel.font = UIFont (name: "Avenir Next Medium", size: 22) 
     tablelabel.textAlignment = .center 
     tablelabel.clipsToBounds = true 
     tablelabel.translatesAutoresizingMaskIntoConstraints = false 
     return tablelabel 
    }() 
    let nameLabel : UILabel = { 
     let tablelabel = UILabel() 
     tablelabel.font = UIFont (name: "Avenir Next Medium", size: 16) 
     tablelabel.textAlignment = .center 
     tablelabel.clipsToBounds = true 
     tablelabel.translatesAutoresizingMaskIntoConstraints = false 
     return tablelabel 
    }() 
    let webUrlLabel : UILabel = { 
     let tablelabel = UILabel() 
     tablelabel.font = UIFont (name: "Avenir Next Medium", size: 16) 
     tablelabel.textAlignment = .center 
     tablelabel.clipsToBounds = true 
     tablelabel.translatesAutoresizingMaskIntoConstraints = false 
     return tablelabel 
    }() 


    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 

     setupViews() 
    } 

    override func layoutSubviews() { 
     super.layoutSubviews() 

     roleLabel.frame = CGRect(x: 0, y: 10, width: self.contentView.bounds.size.width-20, height: 25) 
     nameLabel.frame = CGRect(x: 0, y: roleLabel.frame.origin.y+25, width: self.bounds.size.width-20, height: 25) 
     webUrlLabel.frame = CGRect(x: 0, y: nameLabel.frame.origin.y+25, width: self.bounds.size.width-20, height: 25) 

    } 

    func setupViews(){ 
     contentView.addSubview(roleLabel) 
     contentView.addSubview(nameLabel) 
     contentView.addSubview(webUrlLabel) 
    } 

    func setValuesForCell(contributor : Contributors){ 
     roleLabel.text = contributor.contributorRole 
     nameLabel.text = contributor.contributorName 
     webUrlLabel.text = contributor.contributorWeb 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 

} 

的代码,我写的TableView委托和数据源

extension AboutController : UITableViewDelegate, UITableViewDataSource{ 
    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return contributorList.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell : AboutCell = tableView.dequeueReusableCell(withIdentifier: self.cellid, for: indexPath) as! AboutCell 
     cell.selectionStyle = .default 
     let contributor : Contributors = contributorList[indexPath.row] 
     cell.setValuesForCell(contributor: contributor) 

     return cell 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     print(indexPath.row) 

     tableView.deselectRow(at: indexPath, animated: true) 

    } 
} 

和viewDidLoad方法扩展

override func viewDidLoad() { 
     super.viewDidLoad() 
     tableView.estimatedRowHeight = 100.0 
     tableView.rowHeight = 100 
     tableView.register(AboutCell.self, forCellReuseIdentifier: self.cellid) 

     view.addSubview(tableView) 
     tableView.snp.makeConstraints { (make) in 
      make.top.right.bottom.left.equalToSuperview() 
     } 

     let mayu = Contributors(contibutorRole: "Developer", contributorName: "J Mayooresan", contributorWeb: "http://jaymayu.com") 
     let janie = Contributors(contibutorRole: "Voice Artist", contributorName: "M Jananie", contributorWeb: "http://jaymayu.com") 
     let arjun = Contributors(contibutorRole: "Aathichudi Content", contributorName: "Arjunkumar", contributorWeb: "http://laymansite.com") 
     let artist = Contributors(contibutorRole: "Auvaiyar Art", contributorName: "Alvin", contributorWeb: "https://www.fiverr.com/alvincadiz18") 

     contributorList = [mayu, arjun, janie, artist] 

     tableView.delegate = self 
     tableView.dataSource = self 


     self.tableView.reloadData() 


    } 
+0

看到苹果文档只是重装你的表,当你得到你的数据 –

+0

@HimanshuMoradiya是的,我这样做,已经,但它并没有帮助。 –

+1

@ ReshanKumarasingam:你在哪里设置细胞高度? –

回答

5

由于您使用自动布局布局tableView,因此还需要确保translatesAutoresizingMaskIntoConstraints设置为false

SnapKit应该已经为您设置tableView的translatesAutoresizingMaskIntoConstraintsfalse

由于您手动布局单元(在layoutSubviews中使用框架)。可能不需要将单元子视图的translatesAutoresizingMaskIntoConstraints设置为false

这里translatesAutoresizingMaskIntoConstraints

+0

已经是它的虚假,那么它的影响是如何的限制 –

+0

@ReshanKumarasingam Anbu.Karthik是正确的! SnapKit已经将它设置为false。你做了什么修改来解决这个问题? – ajfigueroa

+0

我不说你的答案是错误的,可能是它固定的问题,你确定它的确切问题影响框架 –

相关问题