0

我有自定义表格视图单元格,其中有两个 UIButton 和一个名为 edit 和 cancel 的文本字段以及一个 textField。当我同时单击编辑时,TextFeild 交互已启用并且取消按钮图像应该更改。它对我来说很好用!!

但是当我单击编辑按钮时,另一个单元格取消 bitton 图像 id 自动更改!我知道发生这种情况是因为我正在重复使用电池!但我无法找到解决方案......

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

编辑:

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

}

4

1 回答 1

0

您的问题是 _textFieldQuantity (和其他)是一个实例变量。当 editQuantity: 和 cancelOreder: 被调用时,实例变量仍然具有上次在 cellForRowAtIndexPath: 中添加单元格时设置的值。

你能做些什么来修复它?第一个:您可以在按钮的标签中设置行。然后,当点击按钮并调用操作时,获取标签并获取单元格(您可以调用表格的 cellForRowAtIndexPath 方法来获取它。这与您实现的委托方法不同),然后找到适当的文本字段。

第二:更多的工作,但可能更聪明的是实现表格单元格的子类。将操作方法​​移动到那个单元类。这样做每个按钮的动作都指向一个单独的对象,然后是单元类而不是视图控制器。单元类可以有自己的 textField 实例变量。它甚至应该为 textField 和其他 UIItems 提供属性,以便您可以在 cellForRowAtIndexPath 中设置它们的值。

我建议将第一个解决方案作为快速入门。但为了将来的使用,我强烈建议采用第二种方式。这种方式将在未来的类似情况下帮助您。

于 2012-12-21T10:47:07.167 回答