【问题标题】:Check That The Button Has Received Touch in Objective-C在 Objective-C 中检查按钮是否被触摸
【发布时间】:2015-08-28 11:41:17
【问题描述】:

我想知道我在这段代码中是否做错了什么:

- (void)checkButtonTapped:(id)sender event:(id)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:tableView];
    NSLog(@"Current: ", currentTouchPosition);
    NSIndexPath *indexPath = [tableView indexPathForRowAtPoint: currentTouchPosition];
    NSLog(@"Index Path: ", indexPath);
    if (indexPath != nil)
    {
        [self tableView: tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
    }
}

indexPath 和 currentTouchPosition 都是空的

我在这里调用方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *sectionTitle = [artistSectionTitles objectAtIndex:indexPath.section];
    NSMutableDictionary *sectionArtists = [artists objectForKey:sectionTitle];
    NSString *title = [sectionArtists objectForKey: [NSString stringWithFormat:@"%i", indexPath.row]];
    NSMutableDictionary *artist = [baseController getCurrentItemByTitle: artistsBD :title];

    NSInteger ID = [artist objectForKey: @"1"];
    static NSString *cellIdentifier = @"listArtists";
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell==nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    [tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"listArtists"];

    cell = [tableView dequeueReusableCellWithIdentifier:@"listArtists"];
    cell.lbName.text = [artist objectForKey: @"2"];
    NSInteger *qtdAlbuns = [artistDAO countAlbuns: ID];
    cell.lbDetails.text = [NSString stringWithFormat:@"%i álbuns", qtdAlbuns];
    cell.icon.image = [UIImage imageNamed: @"icones-categorias-artista"];
    cell.imagePricipal.image = [artistDAO getThumb: ID];

    UIImage *image = [UIImage   imageNamed:@"icon-up"];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    [button setBackgroundImage:image forState:UIControlStateNormal];

    [button addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor clearColor];
    cell.accessoryView = button;

    //cell.accessoryView = [[UIImageView alloc ] initWithImage:[UIImage imageNamed:@"icon-up"]];

    [self setImageCornerRadius : cell];

    cell.tag = ID;
    return cell;
}

按钮图片:http://i.imgur.com/9svY63b.png

当我点击按钮到 currentTouchPosition 并且 indexPath 是空的,所以我想知道我是否在代码中做错了什么。

【问题讨论】:

  • 你到底在做什么?您需要每个 tableview 单元格都具有相同的按钮并且该按钮的工作方式相同吗?
  • 通过点击按钮会展开艺术家的专辑,明白吗? *对不起我的英语不好
  • 我建议你创建自己的 UITableViewCell 类。然后在那个自定义单元格类中,您可以创建您的按钮。何时点击特定单元格视图中的特定按钮,您可以从自定义 UITableViewCell 类调用委托给您的视图控制器。
  • 已解决,我走错了 UITableView。谢谢大家

标签: ios objective-c


【解决方案1】:

实现您想要的更简单的方法是将 UIButton 与块一起使用,例如解释 here

那么您将不需要您的 checkButtonTapped: 方法,而您可以简单地执行以下操作来代替 [button addTarget...]

[button handleControlEvent:UIControlEventTouchUpInside
             withBlock:^{
    [self tableView:tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}];

【讨论】:

    【解决方案2】:

    尝试从按钮向上遍历视图层次结构:

    static UITableViewCell *cellAncestorOfView(UIView *view) {
        while (YES) {
            if (view == nil || [view isKindOfClass:[UITableViewCell class]]) {
                return view;
            }
            view = view.superview;
        }
    }
    
    - (void)checkButtonTapped:(UIButton *)sender event:(UIEvent *)event {
        UITableViewCell *cell = cellAncestorOfView(sender);
        if (cell != nil) {
            NSIndexPath *indexPath = [tableView indexPathForCell:cell];
            if (indexPath != nil) {
                [self tableView:tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-20
      • 2016-07-28
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多