【问题标题】:Getting a button press from a custom UITableViewCell's button从自定义 UITableViewCell 的按钮获取按钮按下
【发布时间】:2012-03-27 11:16:22
【问题描述】:

我有一个带有自定义 UITableViewCell (cell.h/.m/.xib) 的项目,它有 2 个标签 (labelMain/labelSub),上面有 2 个按钮 (buttonName/buttonInfo) 链接到 2 个操作 (showName/showInfo) )。

我想要做的是能够访问我的项目主视图控制器中的 2 个操作,以便当按下 showName 时,视图控制器(不在单元格中)中的 textfield.text 设置为该特定单元格的 labelMain.text .

希望这是有道理的。我的问题是,如果我在 cell.m 中编写操作(showName),我无法从我的主视图控制器访问文本字段。另一方面,如果我在视图控制器中编写动作,我如何知道在哪个单元格中点击了哪个按钮?

希望这是有道理的......

【问题讨论】:

  • 您是否尝试过将标签的弱引用传递给单元格?即 cell.h:@property(非原子,分配)id 文本字段;然后在 cell.m 上写下动作。

标签: iphone objective-c xcode uitableview uibutton


【解决方案1】:

使用tag可以识别出哪个按钮在哪个单元格中被点击了。

- (UITableViewCell *)tableView:(UITableView *)tableViews cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      //init identifier
      if (cell ==nil)
       {
        //load nib file
       }
    
      buttonName.tag = indexPath.row;
      [buttonName addTarget:self action:@selector(showName:) forControlEvents:UIControlEventTouchUpInside];

      buttonInfo.tag = indexPath.row;
      [buttonInfo addTarget:self action:@selector(showInfo:) forControlEvents:UIControlEventTouchUpInside];

    }
}

-(void) showName:(UIButton*)button{
  int row = button.tag; //you know that which row button is tapped
}

-(void)showInfo:(UIButton*)button{
 int row = button.tag;//you know that which row button is tapped
}

---------------- 编辑 -------------

如果您需要根据row & section了解按下了哪个按钮,您可以在下面尝试。 (在cellForRowAtIndexPath:方法中)

int tag = (indexPath.row+1)+(indexPath.section*100);
buttonName.tag = tag;

当按钮在

行 = 5,部分 = 0,然后标签 = 6。

行 = 4,部分 = 3,然后标签 = 305。

行 = 0,部分 = 11,然后标签 =1101。

限制,行数不能超过 99。不要对其他视图使用正标记。如果您需要使用标签,请尝试否定。 (-1, -9, -100)。

所以从这里,您可以计算 indexPath 的后行和部分。使用这个:

-(NSIndexPath*)getIndexPathFromTag:(NSInteger)tag{
    /* To get indexPath from textfeidl tag,
     TextField tag set = (indexPath.row +1) + (indexPath.section*100) */
    int row =0;
    int section =0;
    for (int i =100; i<tag; i=i+100) {
        section++;
    }
    row = tag - (section*100);
    row-=1;
    return  [NSIndexPath indexPathForRow:row inSection:section];
    
}

如何使用:

-(void)showInfo:(UIButton*)button{
     NSIndexPath *indexPath = [self getIndexPathFromTag:button.tag];
     int row = indexPath.row;
     int section = indexPath.section;
}

【讨论】:

  • 在 TableViewController 中以编程方式在哪里添加按钮?
【解决方案2】:

在cellForRowAtIndexPath方法的viewController.m文件中试试这个

[[customCell showNameBtn] setTag:indexPath.row];
[[customCell showNameBtn] addTarget:self action:@selector(showName:) forControlEvents:UIControlEventTouchDown];

-(void) showName:(id)sender
{
// check here the sender tag and then perform you action what you want.
}

【讨论】:

    【解决方案3】:

    我建议为此使用委托(协议)方法。您将完全控制按钮、单元格和视图控制器。而且它也很简单

    【讨论】:

      【解决方案4】:

      我认为最简单的解决方案:

      在你的CustomCell.h:

      @property (nonatomic, copy) void (^buttonTapAction)(id sender);
      

      在您的CustomCell.m 中创建IBAction 并将其连接到您的UIButton:

      -(IBAction)cellButtonIsPressed:(id)sender{
      
          if (self.buttonTapAction) {
              self.buttonTapAction(sender);
          }
      }
      

      然后在您的UITableView 被管理的ViewController.m 中:

          - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
      
              static NSString* cellIdentifier = @"customCell";
              CustomCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
      
              cell.buttonTapAction = ^(id sender){
                      //Do what you want to do when the button inside THIS cell is clicked
                  };
          }
      

      【讨论】:

        【解决方案5】:

        尝试动态创建按钮,其方法在同一个控制器中。在cellForRowAtIndexPath: 方法中执行此操作:

        if (cell == nil) 
        { 
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"WorkRequestedCC" owner:self options:nil];
            {
                for (id oneObject in nib) if ([oneObject isKindOfClass:[WorkRequestedCC class]])
                    cell = (WorkRequestedCC *)oneObject;
        
            }
            UILabel *costLbl=[[UILabel alloc]initWithFrame:CGRectMake(792, 13, 10, 15)];
            costLbl.text=@"$";
            [costLbl setBackgroundColor:[UIColor clearColor]];
            UITextField *txtCost=[[UITextField alloc]initWithFrame:CGRectMake(805, 10, 94, 27)];
            txtCost.backgroundColor=[UIColor whiteColor];
            [txtCost setKeyboardType:UIKeyboardTypeNumberPad];
            txtCost.text=obj.expCost;
        
            [txtCost addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
            [cell.contentView addSubview:costLbl];
            [cell.contentView addSubview:txtCost];
        
            cell.lblDes.text=obj.expDes;
        
        }
        
        - (void)textChanged
        {
             //your code here
        }
        

        【讨论】:

          猜你喜欢
          • 2011-11-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-08
          • 2018-10-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多