Не видя больше вашего кода или того, как у вас настроены ограничения, сложно сказать, что вы делаете неправильно. Однако ...
Вот простой пример трех табличных представлений, расположенных вертикально в scrollView, с вертикальным интервалом между ними 40 пунктов. Кнопки в ячейках работают нормально, независимо от положения прокрутки.
Макет раскадровки выглядит следующим образом:
Результат:
После прокрутки вниз:
код:
//
// 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)
}
}
и источник раскадровки (чтобы помочь вам правильно понять ограничения):
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="llR-vr-4Zy">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Tables In Scroll View Controller-->
<scene sceneID="7Zm-EI-Mg7">
<objects>
<viewController id="llR-vr-4Zy" customClass="TablesInScrollViewController" customModule="SW4Temp" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="tw7-gs-oZR">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="GnA-uY-k8D">
<rect key="frame" x="20" y="40" width="335" height="607"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="PBs-dW-Koi">
<rect key="frame" x="7.5" y="8" width="319" height="128"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="128" id="BKq-4w-98u"/>
</constraints>
</tableView>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="nk6-mF-h3T">
<rect key="frame" x="7.5" y="176" width="319" height="128"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="128" id="Z9y-eh-Yju"/>
</constraints>
</tableView>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="Exi-mx-rcW">
<rect key="frame" x="7.5" y="344" width="319" height="128"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="128" id="qti-gs-wLU"/>
</constraints>
</tableView>
</subviews>
<color key="backgroundColor" red="0.0" green="0.58980089430000004" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="PBs-dW-Koi" firstAttribute="width" secondItem="GnA-uY-k8D" secondAttribute="width" constant="-16" id="Axl-cT-hm3"/>
<constraint firstItem="Exi-mx-rcW" firstAttribute="top" secondItem="nk6-mF-h3T" secondAttribute="bottom" constant="40" id="G1C-SH-ONS"/>
<constraint firstItem="nk6-mF-h3T" firstAttribute="centerX" secondItem="PBs-dW-Koi" secondAttribute="centerX" id="MjW-Yu-9Bi"/>
<constraint firstAttribute="bottom" secondItem="Exi-mx-rcW" secondAttribute="bottom" constant="8" id="R5m-2G-ZHN"/>
<constraint firstItem="PBs-dW-Koi" firstAttribute="top" secondItem="GnA-uY-k8D" secondAttribute="top" constant="8" id="UZj-KF-b8H"/>
<constraint firstItem="PBs-dW-Koi" firstAttribute="leading" secondItem="GnA-uY-k8D" secondAttribute="leading" constant="8" id="bhx-EO-I8M"/>
<constraint firstItem="Exi-mx-rcW" firstAttribute="centerX" secondItem="nk6-mF-h3T" secondAttribute="centerX" id="gF2-Nw-sxy"/>
<constraint firstItem="Exi-mx-rcW" firstAttribute="width" secondItem="nk6-mF-h3T" secondAttribute="width" id="glB-2g-ALO"/>
<constraint firstAttribute="trailing" secondItem="PBs-dW-Koi" secondAttribute="trailing" constant="8" id="n5L-uV-uAN"/>
<constraint firstItem="nk6-mF-h3T" firstAttribute="top" secondItem="PBs-dW-Koi" secondAttribute="bottom" constant="40" id="ppi-8s-Y5S"/>
<constraint firstItem="nk6-mF-h3T" firstAttribute="width" secondItem="PBs-dW-Koi" secondAttribute="width" id="qFz-lA-ieI"/>
</constraints>
</scrollView>
</subviews>
<color key="backgroundColor" red="1" green="0.83234566450000003" blue="0.47320586440000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="V6X-c3-PaY" firstAttribute="trailing" secondItem="GnA-uY-k8D" secondAttribute="trailing" constant="20" id="0Ev-px-vRN"/>
<constraint firstItem="GnA-uY-k8D" firstAttribute="top" secondItem="V6X-c3-PaY" secondAttribute="top" constant="20" id="3sa-DD-wMb"/>
<constraint firstItem="V6X-c3-PaY" firstAttribute="bottom" secondItem="GnA-uY-k8D" secondAttribute="bottom" constant="20" id="Dji-Cc-aaB"/>
<constraint firstItem="GnA-uY-k8D" firstAttribute="leading" secondItem="V6X-c3-PaY" secondAttribute="leading" constant="20" id="ZKZ-mu-117"/>
</constraints>
<viewLayoutGuide key="safeArea" id="V6X-c3-PaY"/>
</view>
<connections>
<outlet property="tableA" destination="PBs-dW-Koi" id="Cnm-we-nuM"/>
<outlet property="tableAHeightConstraint" destination="BKq-4w-98u" id="6mm-8g-BQL"/>
<outlet property="tableB" destination="nk6-mF-h3T" id="yEs-cv-Zld"/>
<outlet property="tableBHeightConstraint" destination="Z9y-eh-Yju" id="oC8-9g-71b"/>
<outlet property="tableC" destination="Exi-mx-rcW" id="UhD-rh-9Kw"/>
<outlet property="tableCHeightConstraint" destination="qti-gs-wLU" id="ej2-mK-5YT"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="kfo-TI-SEb" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="31" y="48"/>
</scene>
</scenes>
</document>