banditKing
banditKing

Reputation: 9579

I want to delete a section from a UITableView when a "delete" button is pressed inside it

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 –

enter image description here

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.

Upvotes: 0

Views: 378

Answers (2)

NIKHIL
NIKHIL

Reputation: 2719

use this delegate method to delete the section or no of sections from the table view

   - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:   (UITableViewRowAnimation)animation;

Hope It Work For You Good Luck

Upvotes: 1

sandy
sandy

Reputation: 1072

I think you should try the button.tag condition. It might be help you something as like given below.

Example

if((((UIButton *)myView).tag == REPLY_BADGE_TAG) || (((UIButton *)myView).tag == FAVORITE_BADGE_TAG))
{
     NSLog(@"tag: %d",((UIButton *)myView).tag);    
} else {
    ((UIButton *)myView).hidden = NO;
    ((UIButton *)myView).tag = indexPath.row;
}  

Upvotes: 1

Related Questions