UITableViewCell按钮混合

时间:2012-12-08 10:15:41

标签: ios5

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

这是我的代码----

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

提前致谢!!

1 个答案:

答案 0 :(得分:1)

对于每个单元格按钮,请为ex:

指定当前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;
 }