2010-04-01 28 views
14

Tôi gặp vấn đề khá lớn. Tôi đang cố gắng tạo nút yêu thích trên mỗi UITableViewCell trong một số UITableView. Điều đó làm việc rất tốt, và tôi hiện đang có một hành động và chọn được thực hiện khi nhấn.Phụ kiện tùy chỉnh UITableViewCell - nhận hàng phụ kiện

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; 

Và 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; 
} 

Bây giờ, đây là vấn đề: Tôi muốn biết những gì chèo phụ kiện đã được ép thuộc về. Tôi có thể làm như thế nào?

Cảm ơn bạn :)

Trả lời

20

Tôi sẽ sử dụng đoạn mã sau:

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

Tôi cũng sử dụng nó. – ZYiOS

16

Thay đổi hành động của bạn xung quanh một chút.

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

Bên trong phương pháp đó:

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

Điều đó sẽ giúp bạn có được con đường chỉ số của hàng.

+1

Cảm ơn bạn rất nhiều - đó là hoạt động hoàn hảo! Cảm ơn bạn! :) – Emil

+1

Vai trò của 'anyObject' ở đây là gì? nó làm gì ? – Illep

+0

Đây là một mẹo tuyệt vời! Cảm ơn bạn. –

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

cảm ơn. hoạt động hoàn hảo – AmiiQo

Các vấn đề liên quan