在屏幕下方加载时,TableViewCell按钮单击未注册

时间:2018-11-06 17:04:47

标签: ios swift xcode uitableview uiscrollview

我的主视图是滚动视图,在此视图内有一个内容视图。在此视图中,我有三个不同的表视图。 tableview的行数可变,我在运行时根据有多少个单元格来设置每个tableView的高度。

例如TableView 1的行高为72,在运行时有三行。所以tableView的高度等于72 * 3 = 216

然后我禁用了TableViews的滚动,这样我就可以轻松浏览整个视图。在每个tableView单元格中都有执行某些操作的按钮。这些按钮使用按钮标签和与每个按钮相关联的动作完美地工作。但这是问题所在...

这些按钮仅在初始加载时出现在屏幕上时才起作用。如果我的滚动视图超出了屏幕底部,则所有按钮都将变为无效状态。无论TableView越过屏幕边缘,下面的所有按钮都将不再起作用。

我已经阅读了一些有关在加载tableView数据之前将内容视图高度设置为滚动视图的整个高度的论坛,但这似乎不起作用。这是我在加载tableData之前用于更改scrollView / contentView高度的代码。我可以显示您认为需要帮助解决此问题的任何其他代码。

totalHeight = self.friendsTableHeight.constant + self.invitationsTableHeight.constant + self.suggestionsTableHeight.constant + 350
self.scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width, height: totalHeight)
self.contentViewHeight.constant = totalHeight

DispatchQueue.main.async {
    self.suggestionsTable.dataSource = self
    self.friendsTableView.dataSource = self
    self.invitationsTableView.dataSource = self
    self.suggestionsTable.reloadData()
    self.friendsTableView.reloadData()
    self.invitationsTableView.reloadData()
}

我还尝试将contentView设置为clipToBounds,但这会切断屏幕下方表格的任何部分。现在已经研究了几个小时,希望以前可能有人遇到过这个问题!

1 个答案:

答案 0 :(得分:0)

在没有看到更多代码或如何设置约束的情况下,很难说出您做错了什么。但是...

下面是一个简单的示例,其中在滚动视图中垂直排列了3个表格视图,它们之间的垂直间距为40点。无论滚动位置如何,单元格中的按钮都可以正常工作。

情节提要布局如下:

enter image description here

结果:

enter image description here

向下滚动后:

enter image description here

代码:

var hostname = window.location.hostname;
var pathname = window.location.pathname;
var i = -1;
do {
  var domain = hostname.substr(i+1);
  var j = 0;
  do {
    var path = pathname.substr(0,j+1);
    var cookie = 'TiddlyWikiClassicOptions=; path='+path+'; domain='+domain+'; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
    console.log(cookie);
    // stacksnippets throws an exception... uncomment to set cookie.
    //document.cookie = cookie;
  } while ((j = pathname.indexOf('/', j+1)) != -1)
} while ((i = hostname.indexOf('.', i+1)) < hostname.lastIndexOf('.'));

和分镜脚本源(帮助您正确设置约束):

//
//  TablesInScrollViewController.swift
//
//  Created by Don Mag on 11/7/18.
//

import UIKit

class ButtonCell: UITableViewCell {

    let theButton: UIButton = {
        let v = UIButton()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.setContentHuggingPriority(.required, for: .horizontal)
        v.backgroundColor = .yellow
        v.setTitleColor(.blue, for: .normal)
        v.setTitle("Button", for: .normal)
        return v
    }()

    let theLabel: UILabel = {
        let v = UILabel()
        v.translatesAutoresizingMaskIntoConstraints = false
        v.backgroundColor = .cyan
        return v
    }()

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

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

    func commonInit() -> Void {

        contentView.addSubview(theLabel)
        contentView.addSubview(theButton)

        NSLayoutConstraint.activate([

            theLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8.0),
            theLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8.0),
            theLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8.0),

            theButton.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8.0),
            theButton.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8.0),
            theButton.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8.0),

            theLabel.trailingAnchor.constraint(equalTo: theButton.leadingAnchor, constant: -8.0),

            ])

        theButton.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
    }

    @objc func buttonTapped(_ sender: Any) {
        print("Button tapped for:", theLabel.text ?? "")
    }

}

class TablesInScrollViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tableA: UITableView!
    @IBOutlet var tableB: UITableView!
    @IBOutlet var tableC: UITableView!

    @IBOutlet var tableAHeightConstraint: NSLayoutConstraint!
    @IBOutlet var tableBHeightConstraint: NSLayoutConstraint!
    @IBOutlet var tableCHeightConstraint: NSLayoutConstraint!

    var aData = ["A - 1", "A - 2", "A - 3", "A - 4", "A - 5", "A - 6"]
    var bData = ["B - 1", "B - 2", "B - 3", "B - 4", "B - 5", "B - 6", "B - 7", "B - 8", "B - 9", "B - 10"]
    var cData = ["C - 1", "C - 2", "C - 3", "C - 4"]

    var rHeight = 72

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let theData = tableView.isEqual(tableA) ? aData : tableView.isEqual(tableB) ? bData : cData
        return theData.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "ButtonCell", for: indexPath) as! ButtonCell

        let theData = tableView.isEqual(tableA) ? aData : tableView.isEqual(tableB) ? bData : cData
        cell.theLabel.text = theData[indexPath.row]

        return cell

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        [tableA, tableB, tableC].forEach {
            $0?.dataSource = self
            $0?.delegate = self
            $0?.register(ButtonCell.self, forCellReuseIdentifier: "ButtonCell")
            $0?.rowHeight = CGFloat(rHeight)
        }

        tableAHeightConstraint.constant = CGFloat(rHeight * aData.count)
        tableBHeightConstraint.constant = CGFloat(rHeight * bData.count)
        tableCHeightConstraint.constant = CGFloat(rHeight * cData.count)

    }

}
相关问题