I am creating a section view programmatically and I have a UIButton “delete” also programmatically assigned. However, when the button is pressed, I’d like to delete that section from my tableview. How should I go about doing this?
Here is the code that creates the section header view:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* customView;
if (Cart.myOrderSummaryRow == 0)
{
// create the parent view that will hold header Label
customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.00)];
customView.backgroundColor = [UIColor grayColor];
//food item label
UILabel * _headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_headerLabel.backgroundColor = [UIColor clearColor];
_headerLabel.opaque = YES;
_headerLabel.textColor = [UIColor blackColor];
//_headerLabel.highlightedTextColor = [UIColor whiteColor];
_headerLabel.font = [UIFont boldSystemFontOfSize:14];
_headerLabel.frame = CGRectMake(40.0, 0.0, 300.0, 44.0);
NSMutableString *header = [[[NSMutableString alloc] initWithCapacity:20] autorelease];
FoodItem *tmpFoodItem = [Cart.foodItemsArray objectAtIndex:section];
[header appendString:tmpFoodItem.foodName];
_headerLabel.text = header;
[customView addSubview:_headerLabel];
//price label
UILabel * _priceLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_priceLabel.backgroundColor = [UIColor clearColor];
_priceLabel.opaque = YES;
_priceLabel.textColor = [UIColor blackColor];
//_headerLabel.highlightedTextColor = [UIColor whiteColor];
_priceLabel.font = [UIFont boldSystemFontOfSize:14];
_priceLabel.frame = CGRectMake(200.0, 0.0, 300.0, 44.0);
NSMutableString *price = [[[NSMutableString alloc] initWithCapacity:20] autorelease];
[price appendString:@"$"];
[price appendString:tmpFoodItem.foodPrice];
_priceLabel.text = price;
[customView addSubview:_priceLabel];
//delete button
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
deleteButton.frame = CGRectMake(260.0, 10, 40.0, 20.0); // x,y,width,height
[deleteButton setTitle:@"-" forState:UIControlStateNormal];
[deleteButton addTarget:self
action:@selector(foodDeleteButtonPressed:)
forControlEvents:UIControlEventTouchDown];
[customView addSubview:deleteButton];
} else {
customView = nil;
}
return customView;
}
Here is how the UITableView looks –

As you can see the sections are in grey and there is a button inside the section for deleting the section itself. The non highlighted part is the row. The sections correspond to food items and the rows correspond to side items. There are different delete buttons for sections and rows.
I think you should try the button.tag condition. It might be help you something as like given below.
Example