【问题标题】:How to create a UIView from a UITableViewCell button click on a separate UIViewController?如何从 UITableViewCell 按钮单击单独的 UIViewController 创建 UIView?
【发布时间】:2020-08-19 14:08:28
【问题描述】:

我正在 Xcode/Swift 中开发一个 iOS 应用程序,当在单独的 UITableViewController 中单击 UITableViewCell 按钮时,我试图在我的 MainViewController 中添加一个子视图 UISendViewButton(它本身就是嵌入UIView)。基本上,这个概念类似于 Instagram 中的“发送帖子”按钮:用户将单击纸飞机按钮,然后出现一个单独的朋友列表(UIView -> UITableViewController)。在联系人列表旁边,有一个按钮 (customButton),用户可以单击该按钮来选择将其发送给哪些朋友。我想要的是只有当用户决定点击他们朋友名字旁边的按钮 (customButton) 时才会出现“发送”按钮 (UIViewButton)。

我能够通过将UITableViewController 嵌入到UIView 中,然后将其作为子视图添加到我的MainViewController 中来显示它,但是当我单击UITableViewCell 类中的customButton 时,什么也没有发生。我希望在单击customButton 时出现一个新的UIViewButton(在我的MainViewController 中)。

所以基本上我想知道如何让这两个控制器进行通信。包含 UITableViewCell 按钮的控制器是 Xcode 库提供的控制器:UITableViewController -> UITableView -> UITableViewCell,它本身嵌入在 UIView 中。

我尝试在下面的 UITableViewCell 类上使用委托:

import UIKit
public protocol CustomCellDelegate: class {

func customButtonClick()

}

class CustomTableViewCell: UITableViewCell {


    @IBOutlet weak var customButton: UIButton!

    weak var delegate: CustomCellDelegate?


    @IBAction func customButtonAction(_ sender: UIButton) {
    
      delegate?.customButtonClick()

    } 
  .. }

以及MainViewController这里对应的代码:

import UIKit
class MainViewController: UIViewController, CustomCellDelegate {

@IBOutlet var UIViewButton: UIView!

 func customButtonClick() {
      self.UIViewButton.frame = CGRect (x:20, y: 300, width: 369, height: 46)
      self.view.addSubview(UIViewButton)

  } }

我知道CustomTableViewCell 在两个不同的控制器中(CustomTableView、CustomTableViewController),所以我认为其中一个控制器中可能存在委托问题/我可能需要为这些控制器添加另一个委托,但是我不确定如何。我设法更改了CustomTableViewCell 中的customButton 图标,所以我知道它是可点击的,但我似乎无法让它代表MainViewController 或与MainViewController 通信并让UIViewButton 出现。对于给您带来的困惑和不便,我深表歉意,由于我是编码初学者,因此将不胜感激。非常感谢!! ^__^

更新:

CustomTableViewController:

public protocol ContactListTableViewControllerDelegate: class {
func showSendButton() }

class CustomTableViewController: UITableViewController, CustomCellDelegate {

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "likesCell", for: indexPath) as! CustomTableViewCell
    cell.delegate = self } 

 func customButtonClick() {
    delegate?.showSendButton()
} }

MainViewController:

extension MainViewController: ContactListTableViewControllerDelegate {
func showSendButton() {
    
       self.UISendButton.frame = CGRect (x:20, y: 300, width: 369, height: 46)
    self.view.addSubview(UISendButton)
} }

【问题讨论】:

  • 如何将主控制器连接到单元格的委托变量?
  • @PhillipMills 非常感谢您的回复!我将 CustomCellDelegate 放在 MainViewController 的类中,所以它看起来像这样:“class MainViewController: UIViewController, CustomCellDelegate {”是你的意思吗?
  • 不,这只是声明您的 MainViewController 能够成为 CustomCellDelegate。需要做的是告诉每个单元格它的delegate 变量应该指向什么对象。

标签: ios swift xcode uitableview delegates


【解决方案1】:

我希望你的 UI 类似于下图。

为此,我会做以下事情

  1. 声明一个协议,让您知道何时显示或隐藏“发送”按钮。 例如

     protocol ContactListTableViewControllerDelegate: class {
         func showSendButton() 
         func hideSendButton()
     }
    
  2. 使第一个视图控制器成为表视图控制器类的委托

  3. 您已经创建了一个协议,用于在单元格内的按钮上拾取点击事件。或者,您还需要将单元格添加为参数,以便委托知道单击了哪个单元格。

     public protocol CustomCellDelegate: class {
    
         func customButtonClick(cell: CustomTableViewCell)
    
     }
    
  4. 在 cellForRowAtIndexPath 方法中创建每个单元格时,使表格视图控制器成为单元格的委托

     cell.delegate = self
    
  5. 在表格视图控制器子类中实现委托方法

     func customButtonClick(cell: CustomTableViewCell) {
         // Find the index path for the clicked cell
         // You should have an array for storing the indices of selected cells
         // Check if the index is already in the array. If yes, remove it from 
         //the array (deselection)
         // If no, add the index to the array(selection)
         // Check the count of the array
         // If it is > 0, it means at least one cell is selected. Call the 
         // delegate method
         // to show button
         delegate?.showSendButton()
         // Else call delegate method to hide send button
         delegate?.hideSendButton()
     } 
    

【讨论】:

  • 为了完整起见,我在此答案中对您的设置做了很多假设。如果您需要更清晰的信息,请告诉我
  • 非常感谢您这么快回复!为了您的方便,我在上面更新了我的原始答案,但仍然遇到困难。我按照您的指示将代表放在正确的位置(我认为),但仍然无法显示按钮/视图。我觉得我在 MainViewController 中缺少一个委托?非常感谢您的帮助
  • 让我们一步一步来,因为我们似乎遗漏了一些有关该问题的基本信息。您将协议 CustomCellDelegate 放在哪个类中?
  • 好的,谢谢。我在我的更新中将协议 CustomCellDelegate 放在上面概述的 CustomTableViewController 类中,如下所示:“ class CustomTableViewController: UITableViewController, CustomCellDelegate { "
  • 好的。您代码中的这一行让我感到困惑“class MainViewController: UIViewController, CustomCellDelegate”
猜你喜欢
  • 2014-05-21
  • 2018-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-30
  • 2014-01-06
相关资源
最近更新 更多