当我在其中按下“删除”按钮时,我想从UITableView中删除一个部分

时间:2011-06-05 17:34:37

标签: iphone cocoa-touch uitableview

我正在以编程方式创建剖面视图,并且我还以编程方式分配了UIButton“删除”。但是,当按下按钮时,我想从我的tableview中删除该部分。我应该怎么做呢?

以下是创建节标题视图的代码:

- (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

正如您所看到的那些部分是灰色的,部分内部有一个按钮用于删除部分本身。未突出显示的部分是行。这些部分对应于食品,行对应于副产品。部分和行有不同的删除按钮。

2 个答案:

答案 0 :(得分:1)

我认为你应该试试button.tag条件。它可能对你有所帮助,如下所示。

示例

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;
}  

答案 1 :(得分:1)

使用此委托方法从表格视图中删除部分或部分

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

希望它对你有用 祝你好运