accessoryButtonTappedForRowWithIndexPath:没有被调用

jim*_*bob 30 objective-c uitableview accessoryview ios

我创建这是越来越使用数组填充的详细披露按钮....然而accessoryButtonTappedForRowWithIndexPath:功能不被称为我的课.这是一个TableviewDelegateTableviewDatasource委托.

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

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

在此输入图像描述

Tom*_*Tom 60

doc说,tableView:accessoryButtonTappedForRowWithIndexPath:当为行设置附件视图时,不会调用该方法indexPath.仅在accessoryView属性出现时nil以及使用和设置accessoryType属性以显示内置附件视图时才调用该方法.

据我了解,accessoryView并且accessoryType相互排斥.使用时accessoryType,系统会tableView:accessoryButtonTappedForRowWithIndexPath:按预期调用,但您必须自己处理其他情况.

Apple执行此操作的方式显示在AccessorySDK 的示例项目中.在cellForRowAtIndexPathdataSource委托的方法中,他们将目标/操作设置为自定义附件按钮.由于无法传递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];
    }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 快速更新 - 看起来如果你将附件类型设置为`DetailDisclosure`而不是简单的`Disclosure`,则会调用`accessoryButtonTappedForRowWithIndexPath`方法并触发segue工作.我无法理解为什么,但至少你可以取得一些进展(即使你的UI看起来不像你想要的那样). (5认同)

mde*_*eus 39

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

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

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


Kei*_*rey 10

将最佳答案转换为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)
    }
}
Run Code Online (Sandbox Code Playgroud)


Avt*_*Avt 9

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

typedef enum : NSInteger {    
UITableViewCellAccessoryNone,   
UITableViewCellAccessoryDisclosureIndicator,   
UITableViewCellAccessoryDetailDisclosureButton,   
UITableViewCellAccessoryCheckmark,   
UITableViewCellAccessoryDetailButton } UITableViewCellAccessoryType;
Run Code Online (Sandbox Code Playgroud)

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

UITableViewCellAccessoryDisclosureIndicator该单元格具有形状像人字形的附件控件.此控件表示点击单元格会触发推送操作.控件不跟踪触摸.

UITableViewCellAccessoryDe​​tailDisclosureButton单元格有一个信息按钮和一个V形图像作为内容.此控件表示点击单元格允许用户配置单元格的内容.控制跟踪触摸.

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

UITableViewCellAccessoryDe​​tailButton单元格有一个没有V形符号的信息按钮.此控件表示点击单元格会显示有关单元格内容的其他信息.控制 跟踪触摸.


小智 7

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

accessoryButtonTappedForRowWithIndexPath 当您单击单元格中的按钮图标而不是选择单元格时,此选项适用.