【问题标题】:accessoryButtonTappedForRowWithIndexPath: not getting called附件ButtonTappedForRowWithIndexPath:没有被调用
【发布时间】:2012-08-31 02:03:38
【问题描述】:

我正在创建一个使用数组填充的详细信息披露按钮....但是在我的类中没有调用accessoryButtonTappedForRowWithIndexPath: 函数。它是 TableviewDelegate 和 TableviewDatasource 代表。

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"reaching accessoryButtonTappedForRowWithIndexPath:");
    [self performSegueWithIdentifier:@"modaltodetails" sender:[self.eventsTable cellForRowAtIndexPath:indexPath]];
}

NSLog 没有打印到控制台,这让我相信函数没有被调用...这当然是当我在单元格上选择时。下面的屏幕截图显示了我如何设置单元格。

【问题讨论】:

  • 您确定将数据源和委托连接到适当的类吗? - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 方法是否被调用?
  • 是 (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 被调用。

标签: ios objective-c uitableview accessoryview


【解决方案1】:

您是否只是单击单元格将其选中,还是您实际单击了单元格上的辅助按钮指示器?从你的问题看不清楚。

accessoryButtonTappedForRowWithIndexPath 适用于单击单元格内的按钮图标而不是选择单元格时。

【讨论】:

  • 您是否进行了所有必要的连接..tableview 数据源和委托给文件所有者?
  • 你找出问题所在了吗?
【解决方案2】:

文档说当为indexPath 的行设置辅助视图时,不会调用方法tableView:accessoryButtonTappedForRowWithIndexPath:。该方法仅在accessoryView 属性为nil 并且使用并设置accessoryType 属性以显示内置附件视图时调用。

据我了解,accessoryView 和 accessoryType 是互斥的。使用accessoryType时,系统会按预期调用tableView:accessoryButtonTappedForRowWithIndexPath:,但其他情况需要您自己处理。

Apple 执行此操作的方式显示在 SDK 的 Accessory 示例项目中。在 dataSource 委托的 cellForRowAtIndexPath 方法中,他们将目标/操作设置为自定义附件按钮。由于无法将indexPath 传递给动作,因此他们调用辅助方法来检索相应的indexPath,并将结果传递给委托方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ...

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    ...

    // set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
    [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
    ...
    cell.accessoryView = button;

    return cell;
}


- (void)checkButtonTapped:(id)sender event:(id)event{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
    if (indexPath != nil){
        [self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
    }
}

由于某种原因,您的设置似乎属于附件视图的情况。您是否尝试使用代码而不是使用 Interface Builder 设置 accessoryType ?

【讨论】:

  • 我遇到了同样的问题,我想知道当您在TableViewCell 检查器窗格。如果您通过单击并拖动也不起作用的披露指示符来创建 segue!
  • 快速更新 - 如果您将附件类型设置为DetailDisclosure 而不仅仅是普通的Disclosure,则accessoryButtonTappedForRowWithIndexPath 方法会被调用并且触发的segue 会起作用。我还不知道为什么,但至少你可以取得一些进展(即使你的 UI 看起来不像你想要的那样)。
  • 那么这是 Xcode 中的错误,还是预期的行为?苹果的文档对此不清楚。表格单元格上有两种类型的披露指示符,普通披露和详细披露,但文档只是说“披露按钮”。
  • 他们为自定义附件按钮设置了目标/操作是不是有点傻?我的意思是,如果我想解决这个问题,那么我只需将 tableviewcell 子类化,并自己在其中添加一个按钮!
【解决方案3】:

画的很到位。

如果您在 Storyboard 中更改为“DetailDisclosure”,则该方法将触发。 (xCode 4.6 DP3)

【讨论】:

    【解决方案4】:

    这似乎很相关......

    披露指示符:当此元素出现时,用户知道他们可以点击行中的任意位置以查看层次结构中的下一级或与列表项关联的选项。选择行在另一个列表的显示中,在一行中使用公开指示符。不要使用披露指标来透露有关列表项的详细信息;为此,请使用详细信息披露按钮。

    详细信息披露按钮:用户点击此元素可查看有关列表项的详细信息。 (请注意,您可以在除表视图之外的视图中使用此元素,以显示有关某些内容的其他详细信息;有关更多信息,请参阅“详细信息披露按钮”。)在表视图中,使用一行中的详细信息披露按钮来显示有关某事的详细信息列表项。请注意,与披露指示器不同,详细信息披露按钮可以执行与选择行分开的操作。例如,在电话收藏夹中,点击该行会向联系人发起呼叫;点击行中的详细信息披露按钮可显示有关联系人的更多信息。

    【解决方案5】:

    根据 UITableViewCellAccessoryType 的文档,这是预期的行为:

    typedef enum : NSInteger {    
    UITableViewCellAccessoryNone,   
    UITableViewCellAccessoryDisclosureIndicator,   
    UITableViewCellAccessoryDetailDisclosureButton,   
    UITableViewCellAccessoryCheckmark,   
    UITableViewCellAccessoryDetailButton } UITableViewCellAccessoryType;
    

    常量 UITableViewCellAccessoryNone 单元格没有任何 附件视图。这是默认值。

    UITableViewCellAccessoryDisclosureIndicator 单元格有一个附件 控制形状像人字形。该控件表示点击 单元格触发推送操作。控件不跟踪触摸。

    UITableViewCellAccessoryDe​​tailDisclosureButton 该单元格有一个信息 按钮和人字形图像作为内容。该控件表明 点击单元格允许用户配置单元格的内容。这 控制跟踪触摸。

    UITableViewCellAccessoryCheckmark 单元格上有一个复选标记 右边。此控件不跟踪触摸。的代表 表格视图可以管理部分行中的复选标记(可能 将复选标记限制为该部分的一行)在其 tableView:didSelectRowAtIndexPath: 方法。

    UITableViewCellAccessoryDe​​tailButton 该单元格有一个信息按钮 没有雪佛龙。此控件表示点击单元格 显示有关单元格内容的附加信息。控制 跟踪触摸。

    【讨论】:

      【解决方案6】:

      这是使用情节提要处理披露按钮的另一种方法。您应该单击表格视图单元格并转到 Connections Inspector。有一个名为 Triggered Segues 的部分,您可以从选择线拖动到您想要 segue 的 UIViewController。 segue 将自动发生,您可以捕获 prepareForSegue 以在发生通知时捕获通知。
      永远不会为显示按钮调用函数 accessoryButtonTappedForRowWithIndexPath。只有详细的披露按钮才会调用它。

      【讨论】:

        【解决方案7】:

        将最佳答案转换为 Swift 3:

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            ...
            let unseletcedImage = UIImage(named: "<imagename>")
            let seletcedImage = UIImage(named: "<imagename>")
            let button = UIButton(type: .custom)
            // match the button's size with the image size
            let frame = CGRect(x: CGFloat(0.0), y: CGFloat(0.0), width: CGFloat((unseletcedImage?.size.width)!), height: CGFloat((unseletcedImage?.size.height)!))
            button.frame = frame
            button.setBackgroundImage(unseletcedImage, for: .normal)
            button.setBackgroundImage(seletcedImage, for: .selected)
            cell?.accessoryView = button
            let action = #selector(checkButtonTapped(sender:event:))
            (cell?.accessoryView as? UIButton)?.addTarget(self, action: action, for: .touchUpInside)
            ....
            return cell!
        
        }
        
        @objc func checkButtonTapped(sender: UIButton?, event: UIEvent) {
            let touches = event.allTouches
            let touch = touches!.first
            guard let touchPosition = touch?.location(in: self.tableView) else {
                return
            }
            if let indexPath = tableView.indexPathForRow(at: touchPosition) {
                tableView(self.tableView, accessoryButtonTappedForRowWith: indexPath)
            }
        }
        

        【讨论】:

          【解决方案8】:

          点击详细信息披露按钮时,未调用我的 tableView 的 accessoryButtonTappedForRowWithIndexPath(…)。我最终发现问题只是没有设置 tableView 委托:

          self.tableView.delegate = self

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-11-06
            • 2017-01-04
            • 2023-03-05
            • 2017-10-20
            • 1970-01-01
            • 2011-11-15
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多