Я хочу удалить раздел из UITableView, когда внутри него нажата кнопка «удалить» - PullRequest
0 голосов
/ 05 июня 2011

Я создаю вид сечения программным путем, и у меня есть 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

Как видите, разделы выделены серым цветом, а внутри раздела есть кнопка для удаления самого раздела.Не выделенная часть - это строка.Разделы соответствуют элементам питания, а строки соответствуют элементам питания.Существуют разные кнопки удаления для разделов и строк.

Ответы [ 2 ]

1 голос
/ 06 июня 2011

используйте этот метод делегата, чтобы удалить раздел или нет разделов из табличного представления.

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

Надеюсь, это сработает, Удачи

1 голос
/ 06 июня 2011

Я думаю, вы должны попробовать условие 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;
}  
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...