0

在我的表格查看单元格中,每个单元格都有两个按钮,编辑和取消,同时单击编辑时,取消按钮应该更改。我的问题是当我尝试单击编辑按钮时另一个单元格取消按钮图像已更改,而不是在我单击的那个单元格上?

这是我的代码----

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath
{
    static NSString *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];

        _checkButton  = [UIButton buttonWithType:UIButtonTypeCustom];
        [_checkButton setFrame:CGRectMake(232, 13, 25, 28)];
        [_checkButton setBackgroundImage:[UIImage imageNamed:@"edit.png"]forState:UIControlStateNormal];
        [_checkButton addTarget:self action:@selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];
        //_checkButton.tag = 0;
        [cell.contentView addSubview:_checkButton];

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

        _textFieldQuantity = [[UITextField alloc] initWithFrame:CGRectMake(125,14,42,21)];
        _textFieldQuantity.userInteractionEnabled = NO;
        [cell addSubview:_cancelButton];
        [cell addSubview:_textFieldQuantity];

    } else {
        cell._nameLabel = (UILabel *)[cell.contentView viewWithTag:0];
        cell._amountMenu = (UILabel *)[cell.contentView viewWithTag:1];
    }
    [_checkButton  setTag:indexPath.row];
    [_cancelButton  setTag:indexPath.row];
    cell._nameLabel.text = [_hotel._orderedMenus objectAtIndex:indexPath.row];
    cell._amountMenu.text  = [[_hotel._menuPrices objectAtIndex:indexPath.row] stringValue];
    _textFieldQuantity.text = [[_hotel._selectedQuantity objectAtIndex:indexPath.row] stringValue];
    return cell;
 }

提前致谢!!

4

1 回答 1

1

对于每个单元格按钮,给出当前 indexPath.row 的标签,例如:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath    *)indexPath
{
    static NSString *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];

        _checkButton  = [UIButton buttonWithType:UIButtonTypeCustom];
        [_checkButton setFrame:CGRectMake(232, 13, 25, 28)];
        [_checkButton setBackgroundImage:[UIImage imageNamed:@"edit.png"]forState:UIControlStateNormal];
        [_checkButton addTarget:self action:@selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];
        _checkButton.tag = [NSString stringWithFormat:@"5%d",indexPath.row];
        [cell.contentView addSubview:_checkButton];

        // 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 addTarget:self  action:@selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];
        _cancelButton.tag = [NSString stringWithFormat:@"6%d",indexPath.row];

        _textFieldQuantity = [[UITextField alloc] initWithFrame:CGRectMake(125,14,42,21)];
        _textFieldQuantity.userInteractionEnabled = NO;
        [cell addSubview:_cancelButton];
        [cell addSubview:_textFieldQuantity];

    } else {
        cell._nameLabel = (UILabel *)[cell.contentView viewWithTag:[NSString stringWithFormat:@"5%d",indexPath.row]];
        cell._amountMenu = (UILabel *)[cell.contentView viewWithTag:[NSString stringWithFormat:@"6%d",indexPath.row]];
    }
    cell._nameLabel.text = [_hotel._orderedMenus objectAtIndex:indexPath.row];
    cell._amountMenu.text  = [[_hotel._menuPrices objectAtIndex:indexPath.row] stringValue];
    _textFieldQuantity.text = [[_hotel._selectedQuantity objectAtIndex:indexPath.row] stringValue];
    return cell;
 }
于 2012-12-08T11:54:49.103 回答