UITableViewCell定制附件 - 获取附件所在行的行号

14

我有一个很大的问题。我正在尝试在UITableView中的每个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;

And选择器:

- (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;
}

现在,问题来了:我想知道被点击的附件所属的行是哪一行。

我应该如何做到这一点?

谢谢 :)


看这个:简单、快速、容易。https://dev59.com/BVkS5IYBdhLWcg3wp4Qi#55156685 - Mushrankhan
3个回答

20

我会使用以下代码:

- (void)didTapStar:(id)sender {
  NSIndexPath *indexPath = [tableView indexPathForCell:(UITableViewCell *)[sender superview]];
}

16

将您的动作进行一些调整。

- (void)action:(id)sender forEvent:(UIEvent *)event

在这个方法里:

NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:[[[event touchesForView:sender] anyObject] locationInView:tableView]];

这将为您获取行的索引路径。


1
anyObject 在这里的作用是什么?它是做什么的? - Illep
@Giao 我只是好奇,anyObject 是什么作用? - Emil
1
@lllep @Emil anyObjectNSSet中的一个实例方法,该方法简单地返回集合中的任何单个对象。在此示例中,[[[event touchesForView:sender] anyObject]返回一个UITouch对象。 - Jeffery Li

1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath {   
    // To set custom cell accessory                     
    UIImage *image = [UIImage   imageNamed:@"white_arrow_right.png"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(cell.frame.size.width  - image.size.width, cell.frame.size.height - image.size.height, image.size.width, image.size.height);
    button.frame = frame;
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button addTarget:self action:@selector(accessoryButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;
}     

- (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];
    }
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{                
    DetailViewController *detailView =[[DetailViewController alloc]init];
    [self.navigationController pushViewController:detailView animated:YES];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接