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

ios - UICollectionView 按钮


                                            <p><p>我正在尝试使用按钮创建 UICollectionView。我可以设置按钮标题,但目标操作不起作用。以下是我的代码:</p>

<pre><code>    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: &#34;TVButtonCell&#34;, for: indexPath) as! TVButtonCell
    cell.channelButton.setTitle(channelKeys, for: .normal)
    cell.channelButton.addTarget(self, action: #selector(FirstViewController.pressed(_:)), for: .primaryActionTriggered)
    return cell
}

    func pressed(_ sender: AnyObject) {
    print(&#34;Button pressed&#34;)
}
</code></pre>

<p>有人知道我做错了什么吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>该死,
需要添加以下功能:</p>

<pre><code>    func collectionView(_ collectionView: UICollectionView, canFocusItemAt indexPath: IndexPath) -&gt; Bool {
    return false
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UICollectionView 按钮,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41048523/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41048523/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UICollectionView 按钮