2011-06-29 56 views
3

我还没有找到关于如何做到这一点的真正好例子。有一张我想用作附件按钮的图像,当我将其放入并点击它时不起作用。因此,它看起来正确的,但没有工作... 这里是我的代码:如何制作自定义表格单元附件

[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton]; 
cell.accessoryView = [[UIImageView alloc] 
    initWithImage:[UIImage imageNamed:@"TableView_Green_Disclosure.png"]]; 

那么,如何让我的UIImageView的调用accessoryButtonTappedForRowWithIndexPath时,它被窃听?

回答

1

检查博客文章hdr-> cmdline以创建UITableView的自定义附件视图。 作者使用UIButton对象与图像的自定义配件视图。

+0

这篇文章的任何链接? :) – Hulvej

+0

PFB链接..http://www.edumobile.org/iphone/iphone-programming-tutorials/impliment-a-custom-accessory-view-for-your-uitableview-in-iphone/ – Jhaliya

2

要使按钮实际上有所作为,您需要从UITableViewDelegate实现- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

当附件按钮连续被窃听,这种方法将被调用,你就会有正确使用索引路径传递,以确定哪些行的附件被窃听行动的机会。

+0

谢谢,但我如何实现,我刚刚提出的imageview,我想我只是假设,因为我指定的配件类型,它会调用该方法? – Jackelope11

5

accessoryViewaccessoryType彻底的阅读就会发现,他们是相互排斥的方式来定制一个细胞。

设置accessoryType将导致在浏览时调用表视图委托方法。

设置accessoryView会忽略的accessoryType设置,给你的东西来显示。如果您想从自定义视图中收到回调,则应该是一个已经连线的控件。 (或使用手势识别器查看任何视图。)

如果您使用按钮并将其操作设置为accessoryTapped:,您将收到该按钮作为“发件人”参数。您可以遍历视图层次结构,直到找到表格视图单元格,然后向表格视图询问该单元格的indexPath。这会让你获得一个你的模型对象的索引,并且你可以适当地采取行动。

作为该按钮的替代,您可以在上面的UIImageView上启用交互,并为其添加手势识别器。

+0

谢谢你通过答案。 – Jackelope11

0

要使用accessoryView - 您需要将单元格的accessoryType设置为UITableViewCellAccessoryNone将UIButton(与关联图像)存入单元格,然后将其连线以接收用户触摸。你可以使用类似下面的代码为IBAction为响应细胞的UIButton的被感动:

- (IBAction) accessoryButtonPressed:(id) sender 
{ 
NSUInteger pathInts[] = { 0,0 }; 
pathInts[1] = self.currentselectedrow; // ivar set when tableview row last selected 
NSIndexPath* indexPath = [NSIndexPath indexPathWithIndexes:pathInts length:2]; 
[self tableView:mytableview accessoryButtonTappedForRowWithIndexPath:indexPath]; 
} 

的UIButton的将有线你的tableview的“的cellForRowAtIndexPath:”里面的线路的方式来执行这种胶码功能

[thecell setButtonTarget:self action:@selector(accessoryButtonPressed:)]; 

我注意到的一件事是,UIButton似乎想要一个'向右滑动'而不是简单的'点击'触摸来触发事件 - 但它可能是我的iOS iOS版本的问题。请注意,我已将一个名为'cell_accessoryButton'的UIButton *对象添加到Custom Cell源。

在细胞的来源,你会支持的代码像这样“setButtonTarget”呼叫:

- (void) setButtonTarget:(MyViewController*)inTarget action:(SEL) inAction 
{ 
[self.cell_accessoryButton addTarget: inTarget 
         action: (SEL) inAction 
      forControlEvents: UIControlEventTouchUpInside]; 
} 

它是如此容易得多只使用accessoryType参考,并让iOS的做繁重的 - 但是,如果你想要一个自定义的图形等 - 这是另一条有效的路径。