0

各セルに1つのテーブルビューがあり、編集とキャンセルとして2つのボタンがあります。同じセル番号で編集ボタンを同時にクリックすると、キャンセル ボタンの画像が変わるはずです。例えば。ユーザーが 2 番目のセルの編集ボタンを選択すると、2 番目のセルのキャンセル ボタンのみがそのイメージを変更する必要があります。

私の問題は、セルの編集ボタンをクリックすると、別のセルのキャンセルボタンの画像が自動的に変更されることです。

ユーザーが最初のセルをクリックしたときに、他のセルではなく最初のセルのキャンセルボタンのみが変更されるようにしたいですか? どうすればこれを行うことができるかサンプルを教えてください。私が知っているように、セルを再利用するためのこのハプニングcozは?

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     simpleTableIdentifier = @"MenuNameCell";
     MenuNameCell *cell = (MenuNameCell *)[tableView    dequeueReusableCellWithIdentifier:simpleTableIdentifier];

      if (cell== nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MenuNameCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        NSLog(@"---------new cell agin");   
    }
       else
    {
        NSArray *arrayView = [cell.contentView subviews];
        for (UIView *vTemp in arrayView)
        {
            [vTemp removeFromSuperview];
        }

        NSLog(@"---No New Cell hiiii");

    }
    // Creating Label Menu Name
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 11, 82, 21)];
    _nameLabel.backgroundColor = [UIColor clearColor];
    _nameLabel.text =  [_hotel._orderedMenus objectAtIndex:indexPath.row];

    // Creating Label Menu Cost
    _amountMenu   = [[UILabel alloc] initWithFrame:CGRectMake(167, 13, 44, 21)];
    _amountMenu.backgroundColor = [UIColor clearColor];
    _amountMenu.text  = [[_hotel._menuPrices objectAtIndex:indexPath.row] stringValue];

    // Creating Text Field For Order Quantity
    _textFieldQuantity = [[UITextField alloc] initWithFrame:CGRectMake(125,14,42,21)];
    _textFieldQuantity.userInteractionEnabled = NO;
    _textFieldQuantity.text = [[_hotel._selectedQuantity objectAtIndex:indexPath.row] stringValue];

    // Creating Button For Check Order
    _checkButton  = [UIButton buttonWithType:UIButtonTypeCustom];
    [_checkButton setFrame:CGRectMake(232, 13, 25, 28)];
    [_checkButton setTag:indexPath.row];
    [_checkButton setBackgroundImage:[UIImage imageNamed:@"edit.png"]forState:UIControlStateNormal];
    [_checkButton addTarget:self action:@selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];


    // Creating Button For CANCEL Order
    _cancelButton  = [UIButton buttonWithType:UIButtonTypeCustom];
    [_cancelButton setFrame:CGRectMake(265, 13, 25, 28)];
    [_cancelButton setBackgroundImage:[UIImage imageNamed:@"cancel.png"] forState:UIControlStateNormal];
    [_cancelButton setTag:indexPath.row];
    [_cancelButton addTarget:self  action:@selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];

    [imageViewArray addObject:_checkButton];
    [imageViewArray addObject:_cancelButton];


    // Adding All To Content View
    [cell.contentView addSubview:_nameLabel];
    [cell.contentView addSubview:_amountMenu];
    [cell.contentView addSubview:_textFieldQuantity];
    [cell.contentView addSubview:_checkButton];
    [cell.contentView addSubview:_cancelButton];


    //objc_setAssociatedObject(_checkButton, iindex, indexPath,OBJC_ASSOCIATION_RETAIN );

    return cell;
    }


      -(void)editQuantity:(id)sender{


    button = (UIButton *)sender;
    row = button.tag;
    col = button.titleLabel.tag;
    _textFieldQuantity.userInteractionEnabled = YES;
    UIImage *buttonImage = [UIImage imageNamed:@"edit_over.png"];
    [_checkButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
    UIImage *buttonImageCancel = [UIImage imageNamed:@"check.png"];
    [_cancelButton setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];
    _cancelButton.tag =  0;

}


     -(void)cancelOreder:(id)sender{


    button = (UIButton *)sender;
    row = button.tag;
    NSLog(@"The Row Selected iS At Cancel Order ISSSS----%d", row);
    if (_cancelButton.tag ==  0){
        _textFieldQuantity.userInteractionEnabled = NO;
        UIImage *buttonImageCancel = [UIImage imageNamed:@"check_over.png"];
        [_cancelButton setBackgroundImage:buttonImageCancel  forState:UIControlStateNormal];
        UIImage *buttonImageCancel1 = [UIImage imageNamed:@"cancel.png"];
        [_cancelButton setBackgroundImage:buttonImageCancel1 forState:UIControlStateNormal];
        UIImage *buttonImage = [UIImage imageNamed:@"edit.png"];
        [_checkButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
        _cancelButton.tag = 1;
    }
    else
    {
        UIAlertView *alert =  [[UIAlertView alloc] initWithTitle:@"iHomeDelivery" message:@"Do You Want To Cancel the Order" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

}

     -(void)removeEntry:(id) sender{
    _hotel = [HotelList sharedInstance];
    NSLog(@"Called...%d",[_hotel._orderedMenus count]);
    NSLog(@"Button index is ------%d", row);
    if ([_hotel._orderedMenus count] > 1) { // if table contains multiple menus
        [_hotel._orderedMenus removeObjectAtIndex:row];
        [_hotel._menuPrices removeObjectAtIndex:row];
        [_hotel._selectedQuantity removeObjectAtIndex:row];
        NSNumber *aNumber;
        int sum = 0;
        for (aNumber in _hotel._menuPrices)
        {
            int aInt = [aNumber intValue];
            sum = sum + aInt;
        }
        _labelFinalBillAmount.text = [NSString stringWithFormat:@"%d", sum];
    }
    else if ([_hotel._orderedMenus count] ==  1){ // if table contain only single menu // Here All array gets Empty
        [_hotel._orderedMenus removeAllObjects];
        _labelFinalBillAmount.text = @"0";
    }
    NSLog(@"--count is %d",[_hotel._orderedMenus count]);
    [_tableviewMenus reloadData];
}
4

2 に答える 2

1

カスタムセルを使用してから、このコードをカスタムクラスに右挿入し、特定のセルの特定の送信者を取得します

-(IBAction)yourButtonMehod:(id)sender
{
    UIView *senderButton = (UIView*) sender;
    NSIndexPath *indexPath = [(UITableView *)self.superView indexPathForCell: (UITableViewCell*)[[senderButton superView]superView]];
}

これで、送信者を取得でき、 indexpath.row を使用して何でもできます

于 2012-12-26T09:48:52.117 に答える
0

ボタンアクションメソッドで

-(IBAction)cellButtonPressed:(id)sender withEvent:(UIEvent *)event
{
    UITouch * touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView: bulletinTable];
    NSIndexPath * indexPath = [bulletinTable indexPathForRowAtPoint: location];
}

そしてあなたの cellForRowAtIndexPath メソッド

[cell.accessory addTarget:self action:@selector(cellButtonPressed:withEvent:) forControlEvents:UIControlEventTouchUpInside];

これはうまくいくはずです。

于 2012-12-26T10:10:47.117 に答える