accessoryButtonTappedForRowWithIndexPath:没有被调用

时间:2012-09-06 09:24:12

标签: ios objective-c uitableview accessoryview

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

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

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

enter image description here

8 个答案:

答案 0 :(得分:57)

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

据我了解,accessoryViewaccessoryType是互斥的。使用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];
    }
}

出于某种原因,您的设置似乎属于accessoryView案例。您是否尝试使用代码而不是使用Interface Builder设置accessoryType

答案 1 :(得分:38)

这似乎非常相关......

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

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

答案 2 :(得分:9)

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

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

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

     

UITableViewCellAccessoryDisclosureIndicator该单元格有一个附件   控制形如雪佛龙。此控件表示轻敲   单元格触发推送操作。控件不会跟踪触摸。

     

UITableViewCellAccessoryDe​​tailDisclosureButton单元格有一个信息   按钮和雪佛龙图像作为内容。此控件表示   点击单元格允许用户配置单元格的内容。该   控制曲目触摸。

     

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

     

UITableViewCellAccessoryDe​​tailButton单元格有一个信息按钮   没有雪佛龙。此控件表示点击单元格   显示有关单元格内容的其他信息。控制   曲目触动。

答案 3 :(得分:9)

将最常见的答案转换为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)
    }
}

答案 4 :(得分:7)

您是否只是单击单元格来选择它,或者您是否实际单击了单元格上的附件按钮指示器?你的问题并不清楚。

当您单击单元格中的按钮图标而不是选择单元格时,

accessoryButtonTappedForRowWithIndexPath适用。

答案 5 :(得分:2)

画了点。

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

答案 6 :(得分:0)

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

答案 7 :(得分:0)

当单击“详细信息”显示按钮时,没有调用我的tableView的accessoryButtonTappedForRowWithIndexPath(…)。最终我发现问题仅在于未设置tableView委托:

self.tableView.delegate = self

相关问题