0

我在每个名为 ad edit and cancel 的单元格中有一个表格视图和两个按钮。当用户同时单击编辑时,同一行取消按钮图像应更改。代码工作正常,但是当我单击第一个单元格编辑按钮时,第三个单元格图像的取消按钮正在更改 insted od 第一个单元格。我该怎么办?这样它就会改变同一个单元格取消按钮的图像..

这是我的代码!!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath    
{

simpleTableIdentifier = @"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");
    // Setting Tag To UIButton
    _checkButton = (UIButton *)[cell.contentView viewWithTag:indexPath.row];
    _cancelButton = (UIButton *)[cell.contentView viewWithTag:indexPath.row];

}
// 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.titleLabel.tag = 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.titleLabel.tag = indexPath.row;
[_cancelButton addTarget:self  action:@selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];


// 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;
NSLog(@"Check Button index is %d",row);
NSLog(@"cehck title is %d",col);
_textFieldQuantity.userInteractionEnabled = YES;
 _textFieldQuantity.text = [[_hotel._selectedQuantity objectAtIndex:row] stringValue];

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

}

4

1 回答 1

0

在 editQuantity 中,看起来您正在更改 _checkButton 和 _cancelButton 的背景图像,但您没有通过它们的标签指定特定实例。

您提到第三行的取消按钮正在更改。这是添加到表中的最后一行吗?

EDITED 在 editQuantity 方法中尝试这样的操作;

UIView *buttonToEdit;
buttonToEdit = [_cancelButton viewWithTag:row];
[buttonToEdit setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];

我尚未对其进行测试,但在这种情况下,buttonToEdit 应该指向您单击的行中的按钮。因此,您将在同一行的按钮上设置背景图像。

希望对你有效。

于 2012-12-24T01:37:28.187 回答