【问题标题】:How to present view controller from UILabel with tap gesture recognized in custom table view cell?如何使用自定义表格视图单元格中识别的点击手势从 UILabel 呈现视图控制器?
【发布时间】:2017-04-04 22:20:00
【问题描述】:

我有一个自定义动态表格视图单元格,其标签添加了已识别的点击手势。当用户点击标签而不是单元格中的其他任何地方时,我想呈现一个视图控制器。

instagram 应用程序具有此功能。 IE。当您点击喜欢时,它会将您带到喜欢表视图,当您点击 cmets 时,它会将您显示到 cmets 表视图。这也是我想要的体验。

我不打算使用 didSelectRow,因为它有点违背了让特定目标区域点击以显示新视图控制器的目的。

那么,如何在 UITableViewCell 的子类中从轻击手势识别器中呈现视图控制器?

更新:

我正在向我的自定义 TableViewCell 传递一个闭包,当按下按钮时它会被成功调用。但是我卡在 TableView 中,无法将信息传递给我要呈现的下一个 View Controller。而且我也不能真正执行转场:\

// From UITableView
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let story = stories[indexPath.row]
    if let cell = tableView.dequeueReusableCell(withIdentifier: "Fun Cell", for: indexPath) as? FunTableViewCell {

        cell.configureCell(title: story.title, info: story.info)

        cell.buttonAction = { [weak self] (cell) in
                print("the button was pressed for \(story.title)")
            self?.buttonWAsTapped(title: story.title)
        }
        return cell
    } else {
        return FunTableViewCell()
    }
}

func buttonWAsTapped(title: String) {
    // Need to pass something to the next View Controller... but how??? 

    if let nextVC = UIViewController() as? DetailViewController {
        nextVC.storyTitle = title
        performSegue(withIdentifier: "Button Pressed", sender: self)
    }
} 


// Custom TableViewCell
class FunTableViewCell: UITableViewCell {

@IBOutlet weak var funLabel: FunLabel!
@IBOutlet weak var standardLabel: TappedLabel!
@IBOutlet weak var funButton: FunButton!

var buttonAction: ((UITableViewCell) -> Void)?

override func awakeFromNib() {
    super.awakeFromNib()

    let tap = UITapGestureRecognizer(target: self, action: #selector(readMoreTapped))
    tap.numberOfTapsRequired = 1
    standardLabel.addGestureRecognizer(tap)
    standardLabel.isUserInteractionEnabled = true

}

@IBAction func btnPressed(sender: UIButton) {
    print("Button pressed")
    buttonAction?(self)
}

【问题讨论】:

  • 确实可行。有什么问题?
  • 如何通过在可重用的表格视图单元格中识别的点击手势呈现视图控制器?

标签: ios uitableview swift3 uitapgesturerecognizer


【解决方案1】:

创建单元格时,将块传递给它。这是button 的处理程序。 点击按钮后,调用该块。
您可以将块保存为UITableViewCell 的子类的属性。

在你的 tableViewCell 类中,添加一个属性:

class CustomTableViewCell: UITableViewCell {

    open var completionHandler: (()->Void)?

}

在具有 tableView 的 viewController 中:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = CustomTableViewCell()
    cell.completionHandler = {
        () -> Void in
        let newViewController = UIViewController()
        //configure the VC here base on the indexPath
        self.present(newViewController, animated: true, completion: nil)
    }
    return cell
}

【讨论】:

  • 感谢您的回答,但我希望在识别轻击手势的 uilabel 中执行此操作,而不是 UIButton。另外,您能否解释或分享一个代码示例,说明您将其传递一个块的含义?
  • @BrianGlowe 带有点击手势识别器的标签几乎是一个按钮。而在swift中,block被称为闭包。
  • 好吧,关于标签和手势识别器,这很公平。我正试图围绕我将如何从闭包呈现视图控制器。很抱歉这对我来说是新的,感谢您的帮助。
  • @BrianGlowe 假设在cellForRowAt 方法中,您需要为行创建单元格。创建单元格后,您可以给它一个块。我会为你写一些示例代码。
  • @BrianGlowe 如果你想配置你的 nextViewController,你应该在prepare(for segue: UIStoryboardSegue, sender: Any?) 中进行。在performSeque 方法中,你不需要传递你的nextVC 对吧?也就是说,你所做的配置根本不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-13
  • 1970-01-01
相关资源
最近更新 更多