iOS User
iOS User

Reputation: 1

UITabel View Cell Button Mixed Up

In my table View Cell Each Cell Has two Button as, Edit And Cancel, On click Of Edit at The Same time Cancel Button Should Change. My Problem Is When I Try To click Edit Button Another cell cancel button image was changed not on that cell which i clicked?

Here Is My Code----

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

Thanks In Advance!!

Upvotes: 0

Views: 99

Answers (1)

Yossi Tsafar
Yossi Tsafar

Reputation: 11

For each cell button, give the tag of the current indexPath.row for ex:

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

Upvotes: 1

Related Questions