1

几天来我遇到了一个问题。问题是我有一个包含多个自定义表格单元格的表格,每个单元格都有多个按钮。但我总是将标签值设为零。下面是我的代码

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CuatomCell_Contacts *cell    =   (CuatomCell_Contacts *) [tableView dequeueReusableCellWithIdentifier:@"cellA"];
    if  (cell == nil)
    {
        NSArray *topLevelObjects            =   [[NSBundle mainBundle] loadNibNamed:@"CuatomCell_Contacts" owner:Nil options:nil];

        for (id currentObject in topLevelObjects)
        {
            if  ([currentObject isKindOfClass:[UITableViewCell class]])
            {
                cell =  (CuatomCell_Contacts *) currentObject;

                break;
            }
        }
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryNone;
    IstructContacts *ObjIstructContacts = nil;
    if (table == self.searchDisplayController.searchResultsTableView)
    {
        ObjIstructContacts = [self.filteredListContent objectAtIndex:indexPath.row];
    }
    else
    {
        ObjIstructContacts = [self.sectionedListContent objectAtIndexPath:indexPath];
    }

    cell.m_CtrlLblName.text = ObjIstructContacts.m_strName;
    //cell.m_CtrlLblContact_Id.text=ObjIstructContacts.m_strContact_Id;
    NSLog(@"%@",ObjIstructContacts.m_strImageUrl);
    if ([ObjIstructContacts.m_strImageUrl isEqualToString:@"No_image"])
    {
        cell.m_CtrlImgImage.image=[UIImage imageNamed:@"Person_Dumy.jpeg"];
    }
    else
    {
        NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: ObjIstructContacts.m_strImageUrl]];

        cell.m_CtrlImgImage.image=[UIImage imageWithData: imageData];
    }
    cell.M_CtrlBtnChat.tag=indexPath.row;
    [cell.M_CtrlBtnChat addTarget:self action:@selector(MoveToNextView:) forControlEvents:UIControlEventTouchUpInside];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self
               action:@selector(MoveToNextView:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Custom Action" forState:UIControlStateNormal];
    button.frame = CGRectMake(150.0f, 5.0f, 150.0f, 30.0f);
    button.tag=indexPath.row;
    [cell.contentView addSubview:button];

    return cell;
}



-(void)MoveToNextView:(id)sender
{
    UIButton *button = (UIButton *)sender;

    NSLog(@"Tag Value = %d",button.tag);
}

标记值始终返回零。在静态和动态按钮操作中。

4

4 回答 4

3

试试这个方法,更简洁的方法来查找视图的封闭行。

 -(IBAction)cellButtonTapped:(id)sender {
    UIButton *button = sender;
    CGPoint correctedPoint = [button convertPoint:button.bounds.origin toView:self.tableview];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:correctedPoint];
    NSLog(@"Button tapped in row %d",indexPath.row);
    }    
于 2014-01-16T08:00:43.243 回答
2

我尝试了一个示例项目,它对我来说很好用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier =@"CustomCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell)
    {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = (UITableViewCell*)[array objectAtIndex:0];
       }

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self
               action:@selector(MoveToNextView:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Custom Action" forState:UIControlStateNormal];
    button.frame = CGRectMake(150.0f, 5.0f, 150.0f, 30.0f);
    button.tag=indexPath.row;
    [cell.contentView addSubview:button];

    return cell;
}
-(void)MoveToNextView:(id)sender
{
    UIButton *button = (UIButton *)sender;

    NSLog(@"Tag Value = %d",button.tag);
}

并在按钮上单击我的日志显示

2013-07-23 11:58:31.719 Trial[895:11303] Tag Value = 0
2013-07-23 11:58:33.726 Trial[895:11303] Tag Value = 1
2013-07-23 11:58:35.126 Trial[895:11303] Tag Value = 2
2013-07-23 11:58:35.998 Trial[895:11303] Tag Value = 3
2013-07-23 11:58:36.910 Trial[895:11303] Tag Value = 4
于 2013-07-23T06:34:26.880 回答
1

尝试使用部分设置标签,如下所示

cell.M_CtrlBtnChat.tag = [[NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row]intValue];

如果您有超过 1 个部分,并且在每个部分中都有 1 行,那么您可能会为所有行获得 0,因此也可以像上面的代码一样获取部分的值..

对于EX:如果您想将行号作为按钮的标签,那么您将其分配给它indexPath.row并且您0每次都会得到,但是如果您使用部分设置它,那么您会得到它,例如,,00等等。如果您有多个部分在每个部分中,您只有一行。1020

更新:现在也设置dequeueReusableCellWithIdentifier为不同的值并检查一下。像下面...

NSString *cellID = [NSString stringWithFormat:@"Section:%d Row:%d",indexPath.section,indexPath.row];
CuatomCell_Contacts *cell    =   (CuatomCell_Contacts *) [tableView dequeueReusableCellWithIdentifier:cellID];
于 2013-07-23T06:13:37.573 回答
-3

您以错误的方式初始化按钮

相反,您的代码如下,

 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
           action:@selector(MoveToNextView:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Custom Action" forState:UIControlStateNormal];
button.frame = CGRectMake(150.0f, 5.0f, 150.0f, 30.0f);
button.tag=indexPath.row;
[cell.contentView addSubview:button];

请尝试使用我的以下代码

 UIButton *button = [UIButton alloc]init];
 button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
           action:@selector(MoveToNextView:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Custom Action" forState:UIControlStateNormal];
button.frame = CGRectMake(150.0f, 5.0f, 150.0f, 30.0f);
button.tag=indexPath.row;
[cell.contentView addSubview:button];

享受编程!

于 2013-07-23T06:11:59.923 回答