gpt4 book ai didi

ios - UITableViewCell 按钮在屏幕下方加载时未注册单击

转载 作者:行者123 更新时间:2023-11-30 11:02:26 28 4
gpt4 key购买 nike

我的主视图是 ScrollView ,在该 View 内我有一个内容 View 。在这个 View 中我有三个不同的表格 View 。 TableView 的行数是可变的,我在运行时根据单元格的数量设置每个 TableView 的高度。

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

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

这些按钮仅在初始加载时位于屏幕上时才起作用。如果我的 ScrollView 超过屏幕底部,所有按钮都会变为非事件状态。无论什么 TableView 超出屏幕边缘,下面的所有按钮都将不再起作用。

我读过一些关于在加载 tableView 数据之前将内容 View 高度设置为 ScrollView 的整个高度的论坛,但这似乎不起作用。这是我在加载 tableData 之前用来更改 ScrollView /内容 View 高度的代码。我可以显示您认为有必要帮助解决该问题的任何其他代码。

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,但这会切断屏幕下方表格的任何部分。现在已经研究了几个小时,希望有人以前遇到过这个!

最佳答案

如果没有看到更多代码或如何设置约束,很难说你做错了什么。然而...

这里是一个简单的例子,在一个scrollView中垂直布局了3个tableView,它们之间有40点的垂直间距。无论滚动位置如何,单元格中的按钮都可以正常工作。

Storyboard布局如下所示:

enter image description here

结果:

enter image description here

向下滚动后:

enter image description here

代码:

//
// 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)

}

}

和 Storyboard源(帮助您获得正确的约束):

<?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>

关于ios - UITableViewCell 按钮在屏幕下方加载时未注册单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53176644/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com