【发布时间】:2011-10-04 15:14:30
【问题描述】:
我在 UITableViewCell 中有一个 UIButton... 我想对 UIButton 执行一个操作..
我需要将数据发送到为此按钮注册的@selector 方法,以便我可以在用户按下此按钮时执行操作。
如何做到这一点,虽然我发现选择器函数中的(id) sender 参数并不总是在cellForRowAtIndexPath 函数中创建的原始按钮。
cellForRowAtIndexPath 函数内部:
UIImage* pdfImage = [UIImage imageNamed:@"icon_pdf.png"];
UIButton* pdfButton = [UIButton buttonWithType:UIButtonTypeCustom];
[pdfButton setImage:pdfImage forState:UIControlStateNormal];
pdfButton.accessibilityHint = @"path/to/pdf/file";
pdfButton.frame = CGRectMake(0, 0, 40, 40);
[pdfButton addTarget:self action:@selector(openUrlInBrowser:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = pdfButton;
这里是发送者函数:
-(void) openUrlInBrowser:(id)sender
{
NSLog(@"%@", [sender class]);
NSString * url=[sender accessibilityHint];
NSLog(@"%@",url);
NSLog(@"%@",sender);
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
}
【问题讨论】:
-
你想让那个按钮覆盖整个单元格吗?
-
查看这些 NSLog 语句的结果会有所帮助。还有你的 cellForRowAtIndexPath 方法的其余部分。
标签: iphone uitableview uibutton