繁体   English   中英

如何从 UITableViewCell 按钮创建 UIView 单击单独的 UIViewController?

[英]How to create a UIView from a UITableViewCell button click on a separate UIViewController?

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

我能够通过将UITableViewController嵌入到UIView ,然后将其作为子视图添加到我的MainViewController中来使它出现,但是当我单击UITableViewCell customButton中的 customButton 时,没有任何反应。 我希望在单击customButton时出现一个新的UIViewButton (在我的MainViewController中)。

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

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

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在两个不同的控制器( CustomTableViewCustomTableViewController )中,所以我认为其中一个可能存在委托问题/我可能需要为这些控制器添加另一个委托,但我不确定如何。 我已经设法更改了CustomTableViewCell中的customButton图标,所以我知道它是可点击的,但我似乎无法让它委托或与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)
} }

我希望您的用户界面类似于下图。

在此处输入图像描述

为此,我会做以下事情

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

     protocol ContactListTableViewControllerDelegate: class { func showSendButton() func hideSendButton() }
  2. 使第一个视图 controller 成为表视图的代表 controller class

  3. 您已经创建了一个协议,用于在单元格内的按钮上拾取点击事件。 您也可以选择将单元格添加为参数,以便代理知道单击了哪个单元格。

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

     cell.delegate = self
  5. 在表视图controller子类中实现委托方法

     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() }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM