代码之家  ›  专栏  ›  技术社区  ›  Itsmeromka

链接ui按钮在swift中点击几个代表

  •  0
  • Itsmeromka  · 技术社区  · 8 年前

    我有一个内部布局 UITableViewCell 存在 UICollectionView 具有 UICollectionViewCell 充满编程生成 UIButton 从视觉上看,它看起来像这样

    enter image description here

    现在我需要在视图控制器中调用一个方法,当我在 ui集合视图单元格 (在我的例子中是 AnswerOptionCell )我发现了几个基本上想法相同的解决方案,可以通过委托访问父代码:

    how to call presentViewController from within a UICollectionViewCell 和; Access a UICollectionView's parent UIViewController

    这是@alexpurtnik代码,以防链接中断

    // UITableViewCell
    
    protocol CustomCellDelegate: class {
        func sharePressed(cell: MyCell)
    }
    
    class CustomCell: UITableViewCell {
        var delegate: CustomCellDelegate?
    
        func didTapShare(sender: UIButton) {
            delegate?.sharePressed(cell: self)
        }
    }
    
    // ViewController
    
    class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
        @IBOutlet weak var tableView: UITableView!
    
        //...
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! CustomCell
            cell.delegate = self
            return cell
        }
    }
    
    extension TableViewController: CustomCellDelegate {
        func sharePressed(cell: CustomCell) {
            guard let index = tableView.indexPath(for: cell)?.row else { return }
            //fetch the dataSource object using index
        }
    }
    

    除了布局深度之外,一切都很好,在上面的示例中,只有两个级别的“嵌套”, UITableView 具有 uiTableView单元格 ViewController 所以他们只是路过 视图控制器 委托给 uiTableView单元格 并达到 视图控制器 代码,在我的情况下 ui集合视图 ui集合视图单元格 委托已设置为 ,所以我不能将其重新分配给 视图控制器 .

    我打算做什么,是从 ui集合视图单元格 调用父级 代码,它将调用父级 视图控制器 方法,它将是某种链子,可能看起来很难看,但可能有效。

    从视觉上看是这样的

    enter image description here

    问题是,会吗?

    更新:

    通过xcode接口设置委托

    enter image description here enter image description here

    1 回复  |  直到 8 年前
        1
  •  0
  •   Itsmeromka    8 年前

    像这样的链子 有用吗? .

    UICollectionViewCell 电话 UITableViewCell 函数,调用 ViewController 功能