how to create custom button on table view cell
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.
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;
}
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-
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-
if(indexPath.row == self.checkedRow)
{
UILabel* label = [[UILabel alloc] init];
//label.text = [NSDate date]; //use date formatters here
[cell.contentView addSubview:label];
}
精彩评论