【问题标题】:Connecting UISwipeGuesture in Storyboard to Table View Controller将 Storyboard 中的 UISwipeGuesture 连接到表视图控制器
【发布时间】:2015-04-30 23:07:57
【问题描述】:

是否需要编写整个函数来获得滑动手势来选择索引行?通常对于表格视图,您将使用indexPathForSelectedRow。但是,如果您不想让用户实际按下单元格然后滑动以获取要在详细视图中加载的正确索引对象,是否有可用的类,情节提要中的连接,或者做你需要自己写吗?

当前选择了正确的对象加载行,然后刷新,但如果我滑动,我会得到默认值,我设置而不是将在indexPathForSelectedRow中出现的内容。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    var upcoming: AnswerTable = segue.destinationViewController as! AnswerTable

    if (segue.identifier == "loadAnswerTableView") {

        let indexPath = self.tableView.indexPathForSelectedRow()

        println(indexPath)

        let obj: PFObject? = self.objectAtIndexPath(indexPath)

    upcoming.parseObject = obj

    self.tableView.deselectRowAtIndexPath(indexPath!, animated: true)

    }

这个prepareForSegue 函数由在界面生成器(故事板)中实现的 UISwipeGesture 触发。因为该函数从let indexPath = self.tableView.indexPathForSelectedRow() 获取要传递到详细视图的数据,所以必须通过用户触摸表格单元格并选择它来物理选择单元格。而不是用户必须这样做,我希望 UI 能够识别我希望将来自被刷过的单元格的数据传递给细节视图控制器。

我确信这可以通过编程方式完成,尽管我还不知道正确的实现方式。但在进入并以编程方式执行之前,我想知道是否有办法通过 Storyboard 将滑动手势分配给 prepareForSegue 方法,或者 UIKit 中是否有一个可以处理此问题的类。

【问题讨论】:

  • 您要问的确切问题是什么?有点模糊

标签: ios objective-c xcode swift cocoa-touch


【解决方案1】:

这是我将如何实施的建议。 我不会将手势附加到 TableView,而是将其附加到 UITableViewCell,并在 cellForRowAtIndexPath 函数中将 UITableViewCell 的标记设置为该行号。并在滑动手势的委托/处理程序中检查哪一行启动了滑动并执行适当的操作。像这样的东西。它的目标 C,但如果您想要基于 SWIFT 的答案,请告诉我,这个概念是相同的。

-(UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     //Create the UITableViewCell //
     cell.contentView.tag = indexPath.row;
     UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] 
     initWithTarget:self action:@selector(handleSwipe:)];
     [gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
     [cell.contentView addGestureRecognizer:swipeGesture];
}

在你的处理函数中

-(void)handleSwipe:(id)sender
{
   UIView *senderView = (UIView *)sender;
   //indexSwiped will have the row which was swiped and you
   //can process what to do in the detail view accordingly
   NSInteger indexSwiped = senderView.tag;
}

【讨论】:

    【解决方案2】:

    如果您使用情节提要并将滑动手势添加到原型单元格,您只需将 tag 添加到单元格并像 Qazi 回答的那样传递它。

    如果您有不同的部分,或者您想传递其他内容,您可以创建一个自定义单元格并在其中添加您想要传递的那些属性。在有不同部分的情况下,您可以创建一个包含indexPath 的属性并将其传递给cellForRowAtIndexPath 中带有cell.indexPath = indexPath 的单元格。

    CustomCell 示例:

    class CustomCell {
        // Some labels
    
        let indexPath: NSIndexPath!
    }
    

    传递给 prepareForSegue 的示例:

    @IBAction func handleSwipe(sender: AnyObject) {
        performSegueWithIdentifier("loadAnswerTableView", sender: sender)
    }
    

    在你的 prepareForSegue 中:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "loadAnswerTableView" {
            let cell = sender!.view as? CustomCell
            let obj: PFObject? = self.objectAtIndexPath(cell.indexPath)
            upcoming.parseObject = obj
        }
    }
    

    【讨论】:

      【解决方案3】:

      以上两个答案都有效。这是我想出的第三个解决方案。

      在玩弄它之后,我能够压缩用相当方便的indexPathForRowAtPoint 编写的行。似乎这种方法是专门为这种类型的处理而编写的。通过将indexPathForSelectedRow 替换为indexPathForRowAtPoint,您可以通过编写一个简单的三行函数将CGFloat 传递给它:

      @IBOutlet var swipeGesture: UISwipeGestureRecognizer!
      
      
      func locationInView(touches: UISwipeGestureRecognizer) -> CGPoint {
      
          var swipeTouch = touches.locationInView(self.view)
      
          return swipeTouch
      
      }
      

      最终代码如下所示:

      func locationInView(touches: UISwipeGestureRecognizer) -> CGPoint {
      
          var swipeTouch = touches.locationInView(self.view)
      
          return swipeTouch
      
      }
      
      
      override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      
          var upcoming: AnswerTable = segue.destinationViewController as! AnswerTable
      
          if (segue.identifier == "loadAnswerTableView") {
      
              let indexPath = self.tableView.indexPathForRowAtPoint(locationInView(swipeGesture))
      
              println(indexPath)
      
              let obj: PFObject? = self.objectAtIndexPath(indexPath)
      
          upcoming.parseObject = obj
      
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多