【问题标题】:UITableViewCell Custom accessory - get the row of accessoryUITableViewCell 自定义附件 - 获取附件的行
【发布时间】:2011-02-03 11:00:53
【问题描述】:

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

和选择器:

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

现在,问题来了:我想知道按下的附件属于哪一行。 我该怎么做?

谢谢你:)

【问题讨论】:

标签: iphone uitableview ios4 row accessory


【解决方案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];
}

【讨论】:

    【解决方案2】:

    我会使用以下代码:

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

    【讨论】:

      【解决方案3】:

      稍微改变一下你的动作。

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

      在那个方法里面:

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

      这应该会为您提供行的索引路径。

      【讨论】:

      • anyObject在这里的作用是什么?它有什么作用?
      • @Giao 我很好奇,anyObject 是做什么的?
      • @lllep @Emil anyObjectNSSet 中的一个实例方法,该方法简单地返回集合中的任何单个对象。在此示例中,[[[event touchesForView:sender] anyObject] 返回一个 UITouch 对象。
      猜你喜欢
      • 2018-10-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-29
      • 2015-03-15
      • 2020-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多