Я создаю вид сечения программным путем, и у меня есть UIButton
«удалить», также программно назначенный.Однако, когда кнопка нажата, я хотел бы удалить этот раздел из моего табличного представления.Как мне сделать это?
Вот код, который создает представление заголовка раздела:
- (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;
}
Вот как выглядит UITableView
-
![enter image description here](https://i.stack.imgur.com/sdEga.png)
Как видите, разделы выделены серым цветом, а внутри раздела есть кнопка для удаления самого раздела.Не выделенная часть - это строка.Разделы соответствуют элементам питания, а строки соответствуют элементам питания.Существуют разные кнопки удаления для разделов и строк.