【问题标题】:UITableViewCell behaves differently in iOS 11UITableViewCell 在 iOS 11 中的行为有所不同
【发布时间】:2017-09-25 04:59:22
【问题描述】:

我以编程方式完成了UITableViewCell,它在 iOS 10 上运行良好。但在使用 iOS 11 和 XCode 9 更新后,它的行为有所不同。布局如下图所示。

但如果我点击单元格,它会重新排列,如下所示。

这里是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()


    }

【问题讨论】:

  • 获取数据后重新加载表格
  • @HimanshuMoradiya 是的,我已经这样做了,但没有帮助。
  • @ReshanKumarasingam:你在哪里设置了单元格高度?
  • 如果你为你的tableView使用自动布局,你是否还需要将translatesAutoresizingMaskIntoConstraints设置为false?此外,如果单元格没有使用自动布局(您正在手动布局视图),那么您不应该触摸translatesAutoresizingMaskIntoConstraints
  • @ajfigueroa 哇!你是对的。它解决了这个问题。您可以将其写为答案,以便我将其标记为正确答案。谢谢。

标签: ios swift uitableview ios-autolayout


【解决方案1】:

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

SnapKit 应该已经为您将 tableView 的 translatesAutoresizingMaskIntoConstraints 设置为 false

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

在此处查看 Apple 文档以获取 translatesAutoresizingMaskIntoConstraints

【讨论】:

  • @ReshanKumarasingam Anbu.Karthik 是正确的! SnapKit 已经为您将其设置为 false。你做了什么改变来解决这个问题?
  • 我不是说你的答案是错误的,可能是它修复了问题,你确定它影响框架的确切问题
  • @ReshanKumarasingam - 确保您的代码也支持 iphone-X
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多