Reputation: 59
I have added a uibutton
in every row of uitableview
, Now, whenever a user clicks on any of the button in uitableview
, I want to remove that button. Please help me to solve this, I have added the button tag with indexPath.row
and then fire a method that contains a mutable array, then I add the [sender tag] into mutable array and reload the tableview and in cell for row, I am checking whether the array contains the object, if yes I place a label else a button.
Upvotes: 1
Views: 1091
Reputation: 6323
add below code in cellForRowAtIndexPath method
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(230, 2, 60, 25);
myButton.tag = indexPath.row;
[myButton setBackgroundColor:[UIColor clearColor]];
[myButton setTitle:@"Click to remove" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:myButton];
//button action metod
-(void)buttonAction:(UIButton *)sender
{
[sender removeFromSuperview];
}
Upvotes: 0
Reputation: 5183
myButton.tag = indexPath.row; is required to identify the UIButton to delete. So, set an unique tag to each created UIButtons in UITableViewCell.
Below is the modified sample code...
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(230, 2, 60, 25);
myButton.tag = indexPath.row;
[myButton setBackgroundColor:[UIColor clearColor]];
[myButton setTitle:@"Button" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:headerButton];
Upvotes: 0
Reputation: 952
I have tried it, hope so this will work for you also
-(IBAction)buttonAction:(id)sender
{
UIButton *mybutton =(UIButton *)sender;
[mybutton removeFromSuperview];
}
- (UITableViewCell *) tableView:(PullDownTableView *)tableView cellInRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = @"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];
}
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(230, 2, 60, 25);
[myButton setBackgroundColor:[UIColor clearColor]];
[myButton setTitle:@"Button" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:headerButton];
return cell;
}
Upvotes: 1
Reputation: 1096
UIButton is a subclass of the UIView class. It has the -removeFromSuperview method. Please note that you will have to check if the cell has had its button removed on the -tableView:cellForRowAtIndexPath method.
Upvotes: 0