首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableViewCell自定义附件-获取附件行

UITableViewCell自定义附件-获取附件行
EN

Stack Overflow用户
提问于 2010-04-02 00:44:04
回答 3查看 12.4K关注 0票数 14

我有一个很大的问题。我试着在UITableView中的每个UITableViewCell上创建一个收藏按钮。这非常好用,我目前有一个动作和选择器在按下时执行。

代码语言:javascript
复制
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;

和选择器:

代码语言:javascript
复制
- (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;
}

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

谢谢您:)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-05-22 19:33:32

我将使用以下代码:

代码语言:javascript
复制
- (void)didTapStar:(id)sender {
  NSIndexPath *indexPath = [tableView indexPathForCell:(UITableViewCell *)[sender superview]];
}
票数 20
EN

Stack Overflow用户

发布于 2010-04-02 01:39:39

稍微改变一下你的行为。

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

在该方法中:

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

这应该会得到行的索引路径。

票数 16
EN

Stack Overflow用户

发布于 2012-07-07 17:30:25

代码语言:javascript
复制
- (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];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2562048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档