为 UITableViewCell 的附件视图使用自定义图像并让它响应 UITableViewDelegate [英] Using a custom image for a UITableViewCell's accessoryView and having it respond to UITableViewDelegate

查看:18
本文介绍了为 UITableViewCell 的附件视图使用自定义图像并让它响应 UITableViewDelegate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义绘制的 UITableViewCell,包括与单元格的 accessoryView 相同的内容.我对附件视图的设置是通过这样的方式发生的:

I'm using a custom drawn UITableViewCell, including the same for the cell's accessoryView. My setup for the accessoryView happens by the way of something like this:

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: 我确保设置以下属性:

Also when the cell is initialized, using initWithFrame:reuseIdentifier: I ensured to set the following property:

self.userInteractionEnabled = YES;

不幸的是,在我的 UITableViewDelegate 中,我的 tableView:accessoryButtonTappedForRowWithIndexPath: 方法(尝试重复 10 次)没有被触发.代表绝对接线正确.

Unfortunately in my UITableViewDelegate, my tableView:accessoryButtonTappedForRowWithIndexPath: method (try repeating that 10 times) is not getting triggered. The delegate is definitely wired up properly.

可能缺少什么?

谢谢大家.

推荐答案

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

Sadly that method doesn't get called unless the internal button type provided when you use one of the predefined types is tapped. To use your own, you'll have to create your accessory as a button or other UIControl subclass (I'd recommend a button using -buttonWithType:UIButtonTypeCustom and setting the button's image, rather than using a UIImageView).

这是我在 Outpost 中使用的一些东西,它自定义了足够多的标准小部件(只是稍微匹配我们的蓝绿色),我最终做了自己的 UITableViewController 中间子类来保存所有其他要使用的表视图的实用程序代码(他们现在继承了 OPTableViewController).

Here's some things I use in Outpost, which customizes enough of the standard widgets (just slightly, to match our teal colouring) that I wound up doing my own UITableViewController intermediary subclass to hold utility code for all other table views to use (they now subclass OPTableViewController).

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

Firstly, this function returns a new detail disclosure button using our custom graphic:

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

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

    return ( button );
}

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

The button will call this routine when it's done, which then feeds the standard UITableViewDelegate routine for accessory buttons:

- (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];
}

此函数通过从按钮提供的事件中获取触摸在表格视图中的位置并询问表格视图在该点处的行的索引路径来定位行.

This function locates the row by getting the location in the table view of a touch from the event provided by the button and asking the table view for the index path of the row at that point.

这篇关于为 UITableViewCell 的附件视图使用自定义图像并让它响应 UITableViewDelegate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆