如何在表视图单元格上创建自定义按钮

时间:2022-07-12 01:27:13

I want to create custom button in table view.in that by clicking on that button the label will be create with current date.In that button i also have to implement checkbox functionality. I have implemented following code for checkbox functionality:It works fine but i can i create label by checking them YES.Thanks in advance.

我想在表视图中创建自定义按钮。通过单击该按钮,将使用当前日期创建标签。在该按钮中,我还必须实现复选框功能。我已经为复选框功能实现了以下代码:它工作正常但我可以通过检查它们来创建标签YES.Thanks提前。

      UIButton *btnUncheck=[[UIButton alloc] initWithFrame:CGRectMake(260, 35, 20, 20)];

    //btnUncheck=[UIButton buttonWithType:UIButtonTypeCustom];
     btnUncheck.tag=indexPath.row;
 [btnUncheck setImage:[UIImage imageNamed:@"NO.png"] forState:UIControlStateNormal];
[btnUncheck addTarget:self action:@selector(checkBoxClicked:)    forControlEvents:UIControlEventTouchUpInside];


  [btnUncheck release];

[cell.contentView addSubview:view];


    return cell;

}

     -(IBAction)checkBoxClicked:(id)sender
      {


   if(favoriteChecked==NO)
{


    [sender setImage:[UIImage imageNamed:@"YES.png"] forState:UIControlStateNormal];
        favoriteChecked=YES;
   [lblAchievementreward setHidden:NO];
        }
else
{


    [sender setImage:[UIImage imageNamed:@"NO.png"] forState:UIControlStateNormal];
    favoriteChecked=NO;
        }

1 个解决方案

#1


1  

Yes, you can. In checkBoxClicked:, save the row of the checked item in an instance variable. Then, reload the row so that cellForRowAtIndexPath is called again. Something like-

是的你可以。在checkBoxClicked:中,将已检查项的行保存在实例变量中。然后,重新加载该行,以便再次调用cellForRowAtIndexPath。就像是-

self.checkedRow = sender.tag;
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathWithIndex:sender.tag]] withRowAnimation:UITableViewRowAnimationNone];

Then, in cellForRowAtIndexPath:, check for self.checkedRow and add a label. Something like-

然后,在cellForRowAtIndexPath:中,检查self.checkedRow并添加标签。就像是-

if(indexPath.row == self.checkedRow)
{
       UILabel* label = [[UILabel alloc] init];
       //label.text = [NSDate date]; //use date formatters here
       [cell.contentView addSubview:label];
}

#1


1  

Yes, you can. In checkBoxClicked:, save the row of the checked item in an instance variable. Then, reload the row so that cellForRowAtIndexPath is called again. Something like-

是的你可以。在checkBoxClicked:中,将已检查项的行保存在实例变量中。然后,重新加载该行,以便再次调用cellForRowAtIndexPath。就像是-

self.checkedRow = sender.tag;
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathWithIndex:sender.tag]] withRowAnimation:UITableViewRowAnimationNone];

Then, in cellForRowAtIndexPath:, check for self.checkedRow and add a label. Something like-

然后,在cellForRowAtIndexPath:中,检查self.checkedRow并添加标签。就像是-

if(indexPath.row == self.checkedRow)
{
       UILabel* label = [[UILabel alloc] init];
       //label.text = [NSDate date]; //use date formatters here
       [cell.contentView addSubview:label];
}