菜鸟教程小白 发表于 2022-12-11 18:57:09

ios - UITableView 项目被突出显示,然后被取消选择 - Swift


                                            <p><p>我有一个带有 3 个嵌套 UIView 的 UIView。它们的总宽度是屏幕的 300%。 (每个 100%)。
我正在更改 AutoLayout 优先级,以便通过从 NavBar 中单击按钮将所需的内容显示在屏幕上,并将其他内容显示在屏幕外。</p>

<p>|uiview1| |uiview2| |uiview3|</p>

<p>uiview3 有一个 UITableView。它填充,完美滚动,但是在点击一个项目时,该项目会突出显示一秒钟然后释放。没有调用函数“didSelectRowAt”。</p>

<p>我已尝试禁用在此 VC 上注册的所有其他手势。点击也会被检测到,但不会持续存在并立即被取消选择。</p>

<pre><code>//globally in VC
@IBOutlet weak var salamsTable: UITableView!

//in viewDidLoad
self.salamsTable.delegate = self
self.salamsTable.dataSource = self

//delegate functions
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
    print (indexPath.row)// this never gets called
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int
{
    return self.allSalam.count      //works perfectly
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell
{
    var cell = self.msgsTable.dequeueReusableCell(withIdentifier: &#34;messages&#34;) as! MessageCell
    return cell // works perfectly
}
</code></pre>

<p> <a href="/image/ZOOzU.png" rel="noreferrer noopener nofollow"><img src="/image/ZOOzU.png" alt="salamsTable is the subject UITableView"/></a> </p>

<p>视觉演示:</p>

<p> <a href="/image/Jy4uE.gif" rel="noreferrer noopener nofollow"><img src="/image/Jy4uE.gif" alt="enter image description here"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>不是解决方案,而是替代方案。事情以另一种方式解决了。正如您在问题中附加的演示 GIF 中看到的那样,单元格被正确突出显示。所以我添加了委托(delegate)方法 didHighlightRowAt,它就像一个魅力。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UITableView 项目被突出显示,然后被取消选择 - Swift,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/46655410/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/46655410/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UITableView 项目被突出显示,然后被取消选择 - Swift