从自定义UITableViewCell按钮按下按钮

时间:2012-03-10 03:16:49

标签: iphone objective-c xcode uitableview uibutton

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

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

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

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

5 个答案:

答案 0 :(得分:16)

使用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
}

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

如果您需要知道基于行和&部分,您可以在下面试试。 (在cellForRowAtIndexPath:方法中)

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

按下

按钮
  

row = 5,section = 0 then tag = 6.

     

row = 4,section = 3 then tag = 305。

     

row = 0,section = 11 then tag = 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;
}

答案 1 :(得分:3)

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.
}

答案 2 :(得分:1)

我建议使用委托(协议)方法。您将完全控制按钮,单元格和视图控制器。它也非常简单

答案 3 :(得分:1)

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

CustomCell.h

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

CustomCell.m创建IBAction并将其与UIButton相关联:

-(IBAction)cellButtonIsPressed:(id)sender{

    if (self.buttonTapAction) {
        self.buttonTapAction(sender);
    }
}

然后在管理ViewController.m的{​​{1}}中:

UITableView

答案 4 :(得分:0)

尝试动态创建按钮,其方法位于同一个控制器中。在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
}