0

I have Custom Table View Cell In That I Have Two UIButton and One Text Field Named As edit and cancel and one textField. When I Click on edit at same time TextFeild Interaction is Enabled And Cancel Button Image Should Change. Its Working Fine For Me!!

But When I am Clicking On Edit Button Another Cells Cancel bitton Image id Changed Automatically! I know that this happening because I'm reusing the cell!! But I'm not able to find a solution...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    simpleTableIdentifier = @"MenuNameCell";
    cell = (MenuNameCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (!cell) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MenuNameCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        NSLog(@"---------new cell agin");          
    }
    else
    {
        for (UIView *view in [cell.contentView subviews])
                [view removeFromSuperview];
        NSLog(@"---------older use");
       // _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 setBackgroundImage:[UIImage imageNamed:@"edit.png"]forState:UIControlStateNormal];
    [_checkButton addTarget:self action:@selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];
    [_checkButton setTag:indexPath.row];

    // 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 setTag:indexPath.row];

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

    return cell;
}

Edit:

-(void)editQuantity:(id)sender 
{ 
    _textFieldQuantity.userInteractionEnabled = YES; 
    button = (UIButton *)sender; 
    row = button.tag; 
    NSLog(@"Check Button index is %d",row); 
    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];
}

}

  • Hmm..., when reusing the cell you delete all of their subviews and in any case you create new subviews from scratch. That should work fine. I'd rahter guess that the error is within your actions. Please provice the ode of the editQuantity: and cancelOreder: (typo here?) methods. – Hermann Klecker Dec 21 '12 at 10:18
  • -(void)editQuantity:(id)sender{ _textFieldQuantity.userInteractionEnabled = YES; button = (UIButton *)sender; row = button.tag; NSLog(@"Check Button index is %d",row); 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; } – Jignesh Kalantri Dec 21 '12 at 10:22
  • -(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.tag = 1; } – Jignesh Kalantri Dec 21 '12 at 10:23
  • I'll add it to your question and format it properly. – Hermann Klecker Dec 21 '12 at 10:33
  • Aparently cancelOreder: is incomplete. Please fix that by editing your question. – Hermann Klecker Dec 21 '12 at 10:38
  • Related: http://stackoverflow.com/questions/9274494/how-to-know-the-uitableview-row-number/9274863#9274863 – jrturton Dec 21 '12 at 11:40

1 Answers1

0

Your problem is that _textFieldQuantity (and the others) is an instance variable. When editQuantity: and cancelOreder: are called the instance varialbe still has the same value that was set last time when adding a cell within cellForRowAtIndexPath: .

What could you do to fix it? 1st: You could set the row in the tag of the button. Then, when the button is tapped on and the action is invoced get the tag and fetch the cell (you can call the table's cellForRowAtIndexPath method to get it. That is different from the delegate method that you implement) and then find the appropriate textField.

2nd: A bit more work but probably smarter could be to implment a subclass of your table cell. Move the action methods to that very cell class. Doing so each button's action refers to an individual object wich is then the cell class and not the view controller. The cell class can have its own textField instance variables. It shoud even provide properties to the textField and other UIItems so that you can set their values in cellForRowAtIndexPath.

I would suggest the first solution as a quick starter. But for future use I strongly suggest to go the second way. That way will help you in similar situations in the future.

Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71
  • can you give me sample code by 2nd approch you suggested, i have edited my code also – Jignesh Kalantri Dec 21 '12 at 11:28
  • Unfortunately I donnot have access to my code before Monday - and that is Christmas eve. This is a tutorial for the 1st approach without subclassing: http://www.theappcodeblog.com/2011/03/28/customize-a-uitableviewcell-in-cellforrowatindexpath-part-1-no-need-to-subclass-uitableviewcell/ And here is an interesting (short) answer given for approach nr. 2 and you find links to related tutorials and guides: http://stackoverflow.com/questions/9014105/where-is-a-good-tutorial-for-making-a-custom-uitableviewcell – Hermann Klecker Dec 21 '12 at 12:28
  • There is all you need in these tutorials. They were quite helpful or me too. – Hermann Klecker Dec 26 '12 at 11:14
  • In the meantime I have reviewed my sample code. Unfortunately that is not too helpful for you because it is overloaded with related functionality that is unique for my specific app. – Hermann Klecker Dec 26 '12 at 11:15