更改详细信息披露按钮的图像(Xcode 4.2)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7797843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing the Image for a detail disclosure button (Xcode 4.2)
提问by Old Dog
I have used the following suggested routine to change the detail disclosure button image in a table view cell (in tableView cellForRowAtIndexPath)
我已使用以下建议例程更改表格视图单元格中的详细信息披露按钮图像(在 tableView cellForRowAtIndexPath 中)
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
UIButton *myAccessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
[myAccessoryButton setBackgroundColor:[UIColor clearColor]];
[myAccessoryButton setImage:[UIImage imageNamed:@"ball"] forState:UIControlStateNormal];
[cell setAccessoryView:myAccessoryButton];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
However, the event tableView accessoryButtonTappedForRowWithIndexPath
is now no longer called on clicking the button.
但是,tableView accessoryButtonTappedForRowWithIndexPath
现在不再在单击按钮时调用该事件。
Has anyone any ideas as to why?
有没有人知道为什么?
回答by Rhubarb
I've +1'd Fry's answer, but just to pull the code in from the other site to make SO more complete: The answer is that you have to accessoryButtonTapped.... call will not be made automatically as it would for the built-in detail-disclosure, so you have to do it yourself with the target of the button.
我已经对 Fry 的回答进行了 +1,但只是为了从其他站点提取代码以使其更加完整:答案是您必须使用附件按钮Tapped .... 不会像对内置详细信息披露,因此您必须自己使用按钮的目标来完成。
Translating from the link code to your example above, add this line after setImage:
将链接代码转换为上面的示例,在 setImage 之后添加以下行:
[myAccessoryButton addTarget:self action:@selector(accessoryButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
Then add this later on:
然后稍后添加:
- (void)accessoryButtonTapped:(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];
}
}
(copied verbatim from this link)
(从这个链接逐字复制)
Note that this assumes that the button is created in the UITableViewController (in my case I'm creating one in a custom cell so the references are slightly different)
请注意,这假设按钮是在 UITableViewController 中创建的(在我的情况下,我在自定义单元格中创建一个,因此引用略有不同)
Explanation: the accessoryButtonTapped is a custom target method for our custom button. When the button is pressed ("TouchUpInside") then we find the indexPath of the cell at the point in which the press occurred, and invoke the method that the table view would ordinarily invoke.
说明:accessoryButtonTapped 是我们自定义按钮的自定义目标方法。当按钮被按下时(“TouchUpInside”),然后我们找到按下发生点的单元格的 indexPath,并调用表格视图通常会调用的方法。