UITableViewCell 自定义附件 - 获取附件行
我有一个很大的问题。我试图在 UITableViewCell
中的每个 UITableViewCell
上创建一个收藏夹按钮。这非常有效,我目前在按下时执行了一个操作和选择器。
accessory = [UIButton buttonWithType:UIButtonTypeCustom];
[accessory setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
accessory.frame = CGRectMake(0, 0, 15, 15);
accessory.userInteractionEnabled = YES;
[accessory addTarget:self action:@selector(didTapStar) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = accessory;
选择器:
- (void) didTapStar {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:/* indexPath? */];
accessory = [UIButton buttonWithType:UIButtonTypeCustom];
[accessory setImage:[UIImage imageNamed:@"stared.png"] forState:UIControlStateNormal];
accessory.frame = CGRectMake(0, 0, 26, 26);
accessory.userInteractionEnabled = YES;
[accessory addTarget:self action:@selector(didTapStar) forControlEvents:UIControlEventTouchDown];
newCell.accessoryView = accessory;
}
现在,问题来了:我想知道被按下的配件属于哪一行。 我该怎么做?
谢谢 :)
I have a pretty big issue. I am trying to create a favorite-button on every UITableViewCell
in a UITableView
. That works very good, and I currently have an action and selector performed when pressed.
accessory = [UIButton buttonWithType:UIButtonTypeCustom];
[accessory setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
accessory.frame = CGRectMake(0, 0, 15, 15);
accessory.userInteractionEnabled = YES;
[accessory addTarget:self action:@selector(didTapStar) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = accessory;
And selector:
- (void) didTapStar {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:/* indexPath? */];
accessory = [UIButton buttonWithType:UIButtonTypeCustom];
[accessory setImage:[UIImage imageNamed:@"stared.png"] forState:UIControlStateNormal];
accessory.frame = CGRectMake(0, 0, 26, 26);
accessory.userInteractionEnabled = YES;
[accessory addTarget:self action:@selector(didTapStar) forControlEvents:UIControlEventTouchDown];
newCell.accessoryView = accessory;
}
Now, here's the problem: I want to know what row the accessory that was pressed belongs to.
How can I do this?
Thank you :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将使用以下代码:
I would use the following code:
稍微改变一下你的动作。
- (void)action:(id)sender forEvent:(UIEvent *)event
在该方法内部:
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:[[[event attemptsForView:sender] anyObject] locationInView :tableView]];
这应该可以让你获得该行的索引路径。
Change your action around a bit.
- (void)action:(id)sender forEvent:(UIEvent *)event
Inside that method:
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:[[[event touchesForView:sender] anyObject] locationInView:tableView]];
That should get you the row's index path.