我有一个很大的问题.我正在尝试在 UITableView
中的每个 UITableViewCell
上创建一个收藏按钮.这很好用,我目前在按下时执行了一个动作和选择器.
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;
和选择器:
- (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?
谢谢你:)
我会使用以下代码:
- (void)didTapStar:(id)sender {
NSIndexPath *indexPath = [tableView indexPathForCell:(UITableViewCell *)[sender superview]];
}
这篇关于UITableViewCell 自定义附件 - 获取附件的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!