【问题标题】:identifying Button selection of each row in Tableview?识别Tableview中每一行的按钮选择?
【发布时间】:2010-12-23 21:30:25
【问题描述】:
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(230.0f, 4.0f, 60.0f, 36.0f);
    [btn setTitle:@"OK" forState:UIControlStateNormal];
    [btn setTitle:@"OK" forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(onClickRename:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:btn];
    
    return cell;
    

    }

但是如果用户选择特定行上的按钮,我可以通过 onClickRename 更改该图像...但是我可以通过哪个行的图像得到触摸

  • (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath)indexPath ?

【问题讨论】:

    标签: iphone


    【解决方案1】:

    在 Swift 中,你可以为每个按钮设置一个标签

    另外,将每个索引值设置为它的标签,我们可以知道在哪一行点击了哪个按钮。

    在 cellForAtIndexPath 中设置标签

    cell.button.tag = indexPath.row
    
    
    in button Action
    
    @IBAction func anAction(_sender : AnyObject){
    
     let tappedButton = sender.tag // gives the row and button
    
    }
    

    【讨论】:

      【解决方案2】:

      Apple 在其Accessory 示例代码中使用了以下技术:

      - (void)checkButtonTapped:(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)
          {
              // do what you want with the item at indexPath
          }
      }
      

      【讨论】:

        【解决方案3】:

        你可以做类似的事情

        [btn setTag:[indexPath] row]
        

        在您的单元设置中,然后

        - (void) onClickRename:(id)sender {
            int row = sender.tag;
        } 
        

        你会知道命中了哪一行。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-02
          • 1970-01-01
          • 2011-12-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多