【问题标题】:UITextView in UITableViewCell prevents segueUITableViewCell 中的 UITextView 防止 segue
【发布时间】:2016-07-02 10:41:26
【问题描述】:

我有许多主要由 UITextViews 组成的 UITableViewCells。

现在的问题是当点击一个单元格时它不会启动-didSelectRowAtIndexPath:

这让我很难转投其他 VC。

我尝试了很多东西,其中之一是添加轻击手势识别器,然后尝试使用-presentViewController: 手动进行segue,我也遇到了一些问题。有没有更通用和简单的方法来做到这一点?我还需要将一些值传递给我将要展示的 viewController。

编辑

这就是我尝试使用手势的方法:

func tap(sender: UITapGestureRecognizer) {

    let tapLocation = sender.locationInView(self.tableView)

    let indexPath = self.tableView.indexPathForRowAtPoint(tapLocation)

    let VC1 = self.storyboard!.instantiateViewControllerWithIdentifier("showPushID")
    let navController = UINavigationController(rootViewController: VC1)
    self.presentViewController(navController, animated:true, completion: nil)

在这里使用上面的这个方法是可行的,但我不能/不知道如何将参数传递给控制器​​上的一些变量。

这个方法:

    let destinationVC = showPushNotificationMessage()
    destinationVC.pushMessage = "test"
    destinationVC.dateOpened = "test"


    presentViewController(destinationVC, animated: true, completion: nil)

给我这个错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

【问题讨论】:

  • 请出示您的代码。
  • @UmairAfzal 更新

标签: ios iphone uitableview uitextview segue


【解决方案1】:

这很简单,伙计:)。方法instantiateViewControllerWithIdentifier 返回UIViewController 所以如果你想访问你的UIViewController 的变量,只需键入你返回的类型如下。

如何将数据传递给您的视图控制器

let destinationVC = self.storyboard!.instantiateViewControllerWithIdentifier("showPushID") as! YourDestinationViewController
destinationVC.pushMessage = "test"
destinationVC.dateOpened = "test"
let navController = UINavigationController(rootViewController: destinationVC)
self.presentViewController(destinationVC, animated:true, completion: nil)

不确定在您的单元格上使用 TextView 的目的,但您可以为您的 CustomTableCell 创建委托方法,然后点击您可以调用您的委托方法,按照步骤操作

创建委托方法

@objc protocol MyCustomCellDelegate
{
    func textViewTapped(cell : MyCustomTableViewCell)
}

自定义表格视图单元格

class MyCustomTableViewCell: UITableViewCell
{
@IBOutlet weak var myTextView : UITextView!
weak var delegate : MyCustomCellDelegate?

override func awakeFromNib() {
    super.awakeFromNib()
    myTextView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.tap(_:))))
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
}

func tap(sender: UITapGestureRecognizer) {
    self.delegate?.textViewTapped(self)
}
}

在您的 ViewController 中 继承 MyCustomCellDelegate,就像您对 UITableViewDelegate 所做的那样 并将委托方法写入您的视图控制器

func textViewTapped(cell: MyCustomTableViewCell)
{
    let vc = self.storyboard!.instantiateViewControllerWithIdentifier("myDestView")
    let navController = UINavigationController(rootViewController: vc)
    self.presentViewController(navController, animated:true, completion: nil)
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell中设置单元格委托

cell.delegate = self

以身份访问您的单元格

let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier") as! AssessmentTableViewCell

快乐编码:)

【讨论】:

  • 工作得很好,谢谢你。也只需编辑您的答案:您编写 self.presentViewController(destinationVC, animated:true, completion: nil),而不是 destinationVC 它应该是 navController
  • 乐于助人
猜你喜欢
  • 2014-05-27
  • 1970-01-01
  • 2011-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-18
相关资源
最近更新 更多