2010-04-01 48 views
14

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

现在,这里的问题:我想知道是什么行被按下属于附件。 我该怎么做?

谢谢:)

回答

20

我会用下面的代码:

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

我也使用这个。 – ZYiOS 2011-08-05 07:43:14

16

改变你的行动一下。

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

内部的方法:

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

这应该让你行的索引路径。

+1

太谢谢你了 - 完美的作品!谢谢! :) – Emil 2010-04-01 18:40:15

+1

'anyObject'在这里有什么作用?它有什么作用 ? – Illep 2011-10-02 17:25:15

+0

这是一个绝妙的把戏!谢谢。 – 2012-09-25 09:49:55

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]; 
} 
+0

谢谢。完美的作品 – AmiiQo 2013-09-03 11:12:16