【问题标题】:Some tableView rows don't show unless I scroll but then disappear除非我滚动但随后消失,否则某些 tableView 行不会显示
【发布时间】:2018-05-10 23:11:21
【问题描述】:

我正在关注(我确实对 tableViewCell 的约束做了一些小改动,我使用的是 stackViewthis RayWenderlich 教程。这是关于动态自调整大小的 tableViewcells。我的问题是我的 tableView 最初加载 3 行(在 iPhone 6s 上)或在 iPhone 7 Plus 上以 2 行开始。仅供参考,当我调试时,我的数据源计数显示它是 4 行。如果我来回滚动几次,那么可能会出现另一行,或者可能两者都显示,或者可能会显示但另一行会消失!

在下面的 gif 中,它最初以 3 行加载屏幕,然后当我滚动时它只显示 2 行,然后再次显示 3 行,但这次第三行不同。 应该始终显示 4 行!

我的模型是一个工作结构

struct Work {
    let title: String
    let image: UIImage
    let info: String
    var isExpanded: Bool
}

还有一个 Artist 结构:

struct Artist {
    let name: String
    let bio: String
    let image: UIImage
    var works: [Work]

    init(name: String, bio: String, image: UIImage, works: [Work]) {
        self.name = name
        self.bio = bio
        self.image = image
        self.works = works
    }
}

我的 WorkTableViewCell 如下:

import UIKit

class WorkTableViewCell: UITableViewCell {

    static let textViewText = "select for more info >"
    var work : Work! {
        didSet{
            titleLabel.text = work.title
            workImageView.image = work.image
        }
    }

    lazy var titleLabel : UILabel = {
        let label = UILabel()
        label.backgroundColor = .cyan
        label.translatesAutoresizingMaskIntoConstraints = false

        return label
    }()

    lazy var workImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
//        imageView.setContentHuggingPriority(1000, for: .vertical)
//         imageView.setContentHuggingPriority(2, for: .vertical)

        return imageView
    }()

    lazy var moreInfoTextView : UITextView = {
        let textView = UITextView()
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.textAlignment = .center
        textView.isScrollEnabled = false

        return textView
    }()
//    
//    override func prepareForReuse() {
//        super.prepareForReuse()
//        
//        imageView?.image = nil
//        
//    }



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

    private func addConstraints(){
        let stackView = UIStackView(arrangedSubviews: [workImageView, titleLabel, moreInfoTextView])
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.axis = .vertical
        stackView.distribution = .fillProportionally
        stackView.alignment = .center

        contentView.addSubview(stackView)
        NSLayoutConstraint.activate([
            stackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
            stackView.topAnchor.constraint(equalTo: contentView.topAnchor),
            stackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
            stackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
            ])

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func awakeFromNib() {
        super.awakeFromNib()

    }
}

这是我的 tableView 的 cellForRowAt 方法:

extension ArtistDetailViewController: UITableViewDataSource{

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! WorkTableViewCell
        cell.work = works[indexPath.row]

        cell.moreInfoTextView.text = works[indexPath.row].isExpanded ? works[indexPath.row].info : WorkTableViewCell.textViewText
        cell.moreInfoTextView.textAlignment = works[indexPath.row].isExpanded ? .left : .center


        return cell
    }

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

我还在viewWillAppearviewDidAppear 中添加了tableView.reloadData(),但没有任何改变。

【问题讨论】:

  • 你的表格视图在滚动视图中吗?我之所以问,是因为滚动条的高度在它成功滚动和反弹之间会发生变化。
  • 不,它只是在 viewController 内部,被限制在它的边缘

标签: ios swift uitableview autolayout uistackview


【解决方案1】:

因此更改 stackView 的 distribution 是解决方法。

我只需要改变:

stackView.distribution = .fillProportionally

到:

stackView.distribution = .fill

我不确定我是否理解为什么这是必要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多