ios - 为 UITableViewCell 的 accessoryView 使用自定义图像并让它响应 UITableViewDelegate

标签 ios cocoa-touch uitableview uikit

我正在使用自定义绘制的 UITableViewCell,包括单元格的 accessoryView。我对 accessoryView 的设置是通过这样的方式进行的:

UIImage *accessoryImage = [UIImage imageNamed:@"accessoryDisclosure.png"];
UIImageView *accImageView = [[UIImageView alloc] initWithImage:accessoryImage];
accImageView.userInteractionEnabled = YES;
[accImageView setFrame:CGRectMake(0, 0, 28.0, 28.0)];
self.accessoryView = accImageView;
[accImageView release];

此外,当使用 initWithFrame:reuseIdentifier: 初始化单元格时,我确保设置以下属性:

self.userInteractionEnabled = YES;

不幸的是,在我的 UITableViewDelegate 中,我的 tableView:accessoryButtonTappedForRowWithIndexPath: 方法(尝试重复 10 次)没有被触发。代表肯定已正确连接。

可能缺少什么?

谢谢大家。

最佳答案

遗憾的是,除非点击使用其中一种预定义类型时提供的内部按钮类型,否则不会调用该方法。要使用你自己的,你必须将你的配件创建为按钮或其他 UIControl 子类(我建议使用 -buttonWithType:UIButtonTypeCustom 按钮并设置按钮的图像,而不是使用 UIImageView ).

这是我在 Outpost 中使用的一些东西,它自定义了足够多的标准小部件(稍微调整一下,以匹配我们的蓝绿色),我最终创建了自己的 UITableViewController 中间子类来保存所有其他表格 View 使用的实用程序代码(它们现在是 OPTableViewController 的子类)。

首先,此函数使用我们的自定义图形返回一个新的详细信息披露按钮:

- (UIButton *) makeDetailDisclosureButton
{
    UIButton * button = [UIButton outpostDetailDisclosureButton];

[button addTarget: self
               action: @selector(accessoryButtonTapped:withEvent:)
     forControlEvents: UIControlEventTouchUpInside];

    return ( button );
}

按钮将在完成后调用此例程,然后为附件按钮提供标准的 UITableViewDelegate 例程:

- (void) accessoryButtonTapped: (UIControl *) button withEvent: (UIEvent *) event
{
    NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: [[[event touchesForView: button] anyObject] locationInView: self.tableView]];
    if ( indexPath == nil )
        return;

    [self.tableView.delegate tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}

此函数通过从按钮提供的事件中获取触摸在 TableView 中的位置并向 TableView 询问该点行的索引路径来定位行。

关于ios - 为 UITableViewCell 的 accessoryView 使用自定义图像并让它响应 UITableViewDelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/869421/

相关文章:

ios - 防止触摸从 UIControl 传递到 ParentView

ios - 为UIImagePickerController设置等效的标签

ios - UITableViewCell subview 在选择时消失(但此 subview 中的内容可见)

ios - 自定义委托(delegate)方法未在其他自定义类中使用其自定义委托(delegate)调用

ios - 如何在带有 cornerRadius 的 UIView 上绘制 1 像素宽的平滑边框?

ios - UIView 之上的 AVPlayer

ios - 在 CGRect iOS 中添加边框

ios - Swift NSAttributedString 修剪

ios - 完全重置 CoreData

ios - 在 UIPanGestureRecognizer 处理程序期间动态更新 UIButton 框架