0

カスタム テーブル ビュー セルがあり、2 つの UIButton と、edit および cancel という名前の 1 つのテキスト フィールドと、1 つの 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;
}
4

2 に答える 2

0

次のコードを使用して、古いセルを再利用せずに新しいセルを直接作成します。

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

それ以外の

cell = (MenuNameCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
于 2012-12-20T12:23:24.940 に答える
0

おい、これはとにかくセルを再利用するためではないと思います。もしあなたが思うなら、すべてのコードをifに保管してください。ただし、クリックイベントでIDを変更するものを確認する必要があります。

もう 1 つ、一般的にこの目的でタグを使用するのが私たちの習慣になっています。ここで、試してみてください:-

* objc_setAssociatedObject*

これにより、常にセルまたは関連するものを参照できます。グーグルだけで、一見すると、下にコードを貼り付けているだけです。チェックしてください:(ただし、要件に従って作業する必要があります)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView != popTbl)
    {
        NSString *CellIdentifier = @"CellIdentifier";
        cell = (CustomizeConditionVC *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomizeConditionVC" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        } 
        cell.lbl.text = [objArr objectAtIndex:indexPath.row];
        [cell.lbl setBackgroundColor:[UIColor clearColor]];
        cell.lbl.font = [UIFont fontWithName:@"Arial" size:(17.0)]; 
        cell.lbl.font = [UIFont boldSystemFontOfSize:17];
        cell.lbl.textColor = [UIColor colorWithRed:0.12 green:0.27 blue:0.34 alpha:1.0]; 
        cell.lbl.autoresizingMask = NO;
        NSString* lblStringLen = NULL;
        lblStringLen = [objArr objectAtIndex:0];
        unsigned int kFactor = -1;
        kFactor = cell.lbl.frame.size.width/[lblStringLen length];
        CGRect rect = cell.lbl.frame;
        rect.size.width = kFactor * [[objArr objectAtIndex:indexPath.row] length];
        [cell.lbl setFrame:rect];
        [cell.txtField setBackgroundColor:[UIColor clearColor]];
        cell.txtField.font = [UIFont fontWithName:@"Arial" size:(17.0)];    
        cell.txtField.textColor = [UIColor colorWithRed:0.50 green:0.50 blue:0.50 alpha:1.0]; 

        CGRect rectTxtField = [cell.txtField frame];
        rectTxtField.size.height = 27;
        [cell.txtField setFrame:rectTxtField];
        [cell.txtField setDelegate:self];
        [cell.txtField setTag:indexPath.row];

        [cell.txtView setBackgroundColor:[UIColor clearColor]];
        [cell.txtView setBackgroundColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0]];
        cell.txtView.font = [UIFont fontWithName:@"Arial" size:(17.0)]; 
        cell.txtView.textColor = [UIColor colorWithRed:0.50 green:0.50 blue:0.50 alpha:1.0]; 
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        [cell.btn1 setTag:1];
        [cell.btn2 setTag:2];
        [cell.btn3 setTag:3];
        [cell.btn1 addTarget:self action:@selector(ButtonAtCellControl:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btn2 addTarget:self action:@selector(ButtonAtCellControl:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btn3 addTarget:self action:@selector(ButtonAtCellControl:) forControlEvents:UIControlEventTouchUpInside];

        [cell.btnClearence addTarget:self action:@selector(ButtonAtCellControl:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btnMaintenance addTarget:self action:@selector(ButtonAtCellControl:) forControlEvents:UIControlEventTouchUpInside];
        [cell.btnClearence setHidden:YES];
        [cell.btnMaintenance setHidden:YES];

//        //////////////
        objCell = cell;
//        //////////////
        int getVersionFromCategory = [[self ExtractTheFirstCharBeforePeriodOfDevice] intValue];

       // cell.btn1 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn1.frame];

        switch (indexPath.row) 
        {
            case 2:
            case 4:
                cell.txtView = [UITextView SetTheBorderProperties:cell.txtView];
                [cell.txtView setHidden:NO];
                [cell.txtView setDelegate:self];
                [cell.txtField setHidden:YES];
                [cell.smallBtn setHidden:YES];
                [cell.largeBtn setHidden:YES];
                [cell.txtView setFrame:CGRectMake(cell.lbl.frame.origin.x+13, cell.lbl.frame.origin.y + cell.lbl.frame.size.height + 3, 295-39, (121-(cell.lbl.frame.origin.y + cell.lbl.frame.size.height +3)+89))];
                [cell.btn1 setHidden:YES];
                [cell.btn2 setHidden:YES];
                [cell.btn3 setHidden:YES];
                if(indexPath.row == 4)
                {
                    [cell.txtView setHidden:YES];
                    [cell.btnClearence setHidden:NO];
                    [cell.btnMaintenance setHidden:NO];
                } 
                 break;
            case 3:
                [cell.btn1 setHidden:NO];
                prototype1 = btn1 = cell.btn1;
                /////////New Entry on Date:Nov.07...
                prototype2 = cell.btn2;
                prototype3 = cell.btn3;
                ////////
                [cell.btn2 setHidden:NO];
                [cell.btn3 setHidden:NO];
                [cell.btn1 setFrame:CGRectMake(cell.txtField.frame.origin.x, cell.txtField.frame.origin.y, cell.txtField.frame.size.width/3, cell.txtField.frame.size.height)];
                [cell.btn2 setFrame:CGRectMake(5+cell.txtField.frame.origin.x+cell.txtField.frame.size.width/3+5, cell.txtField.frame.origin.y, cell.txtField.frame.size.width/3, cell.txtField.frame.size.height)];
                [cell.btn3 setFrame:CGRectMake(15 + cell.txtField.frame.origin.x+2*(cell.txtField.frame.size.width/3)+5, cell.txtField.frame.origin.y, cell.txtField.frame.size.width/3, cell.txtField.frame.size.height)];
                [cell.txtField setHidden:YES];
                [cell.smallBtn setHidden:YES];
                [cell.largeBtn setHidden:YES];

//                UIAlertView *alert = [[UIAlertView alloc]
//                                      initWithTitle:@"Hover display.." message:[NSString stringWithFormat:@"%d",cell.btn1.frame.size.width] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
//                [alert show];
//                [alert release];
                break;
            default:
                [cell.btn1 setHidden:YES];
                [cell.btn2 setHidden:YES];
                [cell.btn3 setHidden:YES];
                [cell.txtView setHidden:YES];
                [cell.txtView setDelegate:nil];
                [cell.smallBtn addTarget:self action:@selector(DepictingPopUp:) forControlEvents:UIControlEventTouchUpInside];
                [cell.largeBtn addTarget:self action:@selector(DepictingPopUp:) forControlEvents:UIControlEventTouchUpInside];
                objc_setAssociatedObject(cell.smallBtn,SMALL_BTN_REF,indexPath,OBJC_ASSOCIATION_RETAIN);
                objc_setAssociatedObject(cell.largeBtn,SMALL_BTN_REF_1,indexPath,OBJC_ASSOCIATION_RETAIN);
                objc_setAssociatedObject(cell.largeBtn, LARGE_BTN_REF, cell.txtField, OBJC_ASSOCIATION_RETAIN);
                objc_setAssociatedObject(cell.smallBtn, CELL_TO_CELL_REF, cell.txtField, OBJC_ASSOCIATION_RETAIN);
                break;
        }

//        int getVersionFromCategory = [[self ExtractTheFirstCharBeforePeriodOfDevice] intValue];
//        if(getVersionFromCategory > 3)
//        {
//            cell.btn1 = [UIButton MButton3DLook:cell.btn1 AndButtonTitle:@"gdf"];
//            cell.btn2 = [UIButton MButton3DLook:cell.btn2 AndButtonTitle:@"gdf"];
//            cell.btn3 = [UIButton MButton3DLook:cell.btn3 AndButtonTitle:@"gdf"];
//        }
//        cell.btn1 = [UIButton MButton3DLook:cell.btn1 AndButtonTitle:@"gdf"];
//        cell.btn2 = [UIButton MButton3DLook:cell.btn2 AndButtonTitle:@"gdf"];
//        cell.btn3 = [UIButton MButton3DLook:cell.btn3 AndButtonTitle:@"gdf"];

        if([[NSUserDefaults standardUserDefaults] boolForKey:@"PreviewCase"])
        {
            switch (indexPath.row) 
            {
                case 0:
                    cell.txtField.text = [(NSMutableDictionary*)getTheConditionListVC objectForKey:@"qty"];
                    break;
                case 1:
                    //cell.txtField.text = [(NSMutableDictionary*)getTheConditionListVC objectForKey:@"extraCol"];
                    cell.txtField.text = [(NSMutableDictionary*)getTheConditionListVC objectForKey:@"condition"];
                    break;
                case 2:
                    cell.txtField.text = [(NSMutableDictionary*)getTheConditionListVC objectForKey:@"furtherNote"];
                    cell.txtView.text = [(NSMutableDictionary*)getTheConditionListVC objectForKey:@"furtherNote"];
                    break;
                case 3://liability//+(UIButton*)MButton3DLookForSelected:(UIButton*)_rectForButton AndButtonTitle:(NSString*)_btnTitle
                {
                    UIAlertView *alert = [[UIAlertView alloc]
                                          initWithTitle:@"Hover display.." message:[(NSMutableDictionary*)getTheConditionListVC objectForKey:@"liability"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                    //[alert show];
                    [alert release];
                    if(getVersionFromCategory > 3)
                    {
                        switch ([[(NSMutableDictionary*)getTheConditionListVC objectForKey:@"liability"] intValue]) {
                            case 1:
                            //case 0:
                                cell.btn1 = [UIButton MButton3DLookForSelected:cell.btn1 AndButtonTitle:@"gdf"];
                                cell.btn2 = [UIButton MButton3DLook:cell.btn2 AndButtonTitle:@"gdf"];
                                cell.btn3 = [UIButton MButton3DLook:cell.btn3 AndButtonTitle:@"gdf"];
                                break;
                            case 2:
                                cell.btn1 = [UIButton MButton3DLook:cell.btn1 AndButtonTitle:@"gdf"];
                                cell.btn2 = [UIButton MButton3DLookForSelected:cell.btn2 AndButtonTitle:@"gdf"];
                                cell.btn3 = [UIButton MButton3DLook:cell.btn3 AndButtonTitle:@"gdf"];
                                break;
                            case 3:
                                cell.btn1 = [UIButton MButton3DLook:cell.btn1 AndButtonTitle:@"gdf"];
                                cell.btn2 = [UIButton MButton3DLook:cell.btn2 AndButtonTitle:@"gdf"];
                                cell.btn3 = [UIButton MButton3DLookForSelected:cell.btn3 AndButtonTitle:@"gdf"];
                                break;
                            default:
                                break;
                        }

                    }
//                    else
//                    {
//                        switch ([[(NSMutableDictionary*)getTheConditionListVC objectForKey:@"liability"] intValue]) {
//                            case 1:
//                                //case 0:
//                                cell.btn1 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn1.frame];
//                                cell.btn2 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn2.frame];
//                                cell.btn3 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn3.frame];
//                                break;
//                            case 2:
//                                cell.btn1 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn1.frame];
//                                cell.btn2 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn2.frame];
//                                cell.btn3 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn3.frame];
//                                break;
//                            case 3:
//                                cell.btn1 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn1.frame];
//                                cell.btn2 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn2.frame];
//                                cell.btn3 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn3.frame];
//                                break;
//                            default:
//                                break;
//                        }
//                    }
                }
                    break;
                case 4:
                    if([[(NSMutableDictionary*)getTheConditionListVC objectForKey:@"extraCol"] intValue] == 4)
                    {
                        //////////////
                        objCell = cell;
                        //////////////

                        [[(CustomizeConditionVC*)objCell btnClearence] setImage:[UIImage imageNamed:@"brush_selected.png"] forState:UIControlStateNormal];
                        [[(CustomizeConditionVC*)objCell btnMaintenance] setImage:[UIImage imageNamed:@"cross.png"] forState:UIControlStateNormal];
                    }
                    else 
                    if([[(NSMutableDictionary*)getTheConditionListVC objectForKey:@"extraCol"] intValue] == 5)
                    {
                        //////////////
                        objCell = cell;
                        //////////////

                        [[(CustomizeConditionVC*)objCell btnClearence] setImage:[UIImage imageNamed:@"brush.png"] forState:UIControlStateNormal];
                        [[(CustomizeConditionVC*)objCell btnMaintenance] setImage:[UIImage imageNamed:@"cross_selected.png"] forState:UIControlStateNormal];
                    }   
                    break;
                default:
                    break;
            }
        }
        else {
            //int getVersionFromCategory = [[self ExtractTheFirstCharBeforePeriodOfDevice] intValue];
            if(getVersionFromCategory > 3)
            {
                cell.btn1 = [UIButton MButton3DLook:cell.btn1 AndButtonTitle:@"gdf"];
                cell.btn2 = [UIButton MButton3DLook:cell.btn2 AndButtonTitle:@"gdf"];
                cell.btn3 = [UIButton MButton3DLook:cell.btn3 AndButtonTitle:@"gdf"];
            }
            else
            {
//                [cell.btn1 setFrame:CGRectMake(cell.txtField.frame.origin.x, cell.txtField.frame.origin.y, cell.txtField.frame.size.width/3, cell.txtField.frame.size.height)];
//                [cell.btn2 setFrame:CGRectMake(5+cell.txtField.frame.origin.x+cell.txtField.frame.size.width/3+5, cell.txtField.frame.origin.y, cell.txtField.frame.size.width/3, cell.txtField.frame.size.height)];
//                [cell.btn3 setFrame:CGRectMake(15 + cell.txtField.frame.origin.x+2*(cell.txtField.frame.size.width/3)+5, cell.txtField.frame.origin.y, cell.txtField.frame.size.width/3, cell.txtField.frame.size.height)];
//                [cell.txtField setHidden:YES];
//                [cell.smallBtn setHidden:YES];
//                [cell.largeBtn setHidden:YES];
                //MCustomizeButtonRect


           /*     
                cell.btn1 = [[MCustomizeButtonRect alloc] initWithFrame:prototype1.frame];
                cell.btn2 = [[MCustomizeButtonRect alloc] initWithFrame:prototype2.frame];
                cell.btn3 = [[MCustomizeButtonRect alloc] initWithFrame:prototype3.frame];

            */    

//                UIAlertView *alert = [[UIAlertView alloc]
//                                      initWithTitle:@"Hover display.." message:[NSString stringWithFormat:@"%d",cell.btn1.frame.size.width] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
//                [alert show];
//                [alert release];

            }
        }
//        prototype1 = cell.btn1;
//        prototype2 = cell.btn2;
//        prototype3 = cell.btn3;
        return cell;
    }
    else 
    {
        static NSString *CellIdentifier = @"Cell"; 
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(50,50, 201, 47) 
                                           reuseIdentifier:CellIdentifier] autorelease]; 
        cell.textLabel.text = @"N/A";
        NSString* dropMenusString = NULL;
        dropMenusString = [[tempOfArr objectAtIndex:indexPath.row] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([dropMenusString length] > 0) 
            cell.textLabel.text = dropMenusString;
        //cell.textLabel.text = [[tempOfArr objectAtIndex:indexPath.row] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        return cell;
    }
}

Now, access your association as :-

    -(void)DepictingPopUp:(id)sender
    {
        [popTbl isHidden]?[popTbl setHidden:NO]:[popTbl setHidden:YES];
        [popTbl setHidden:NO];
        NSIndexPath* iPath =  nil;
        iPath = objc_getAssociatedObject(sender, SMALL_BTN_REF);
        NSIndexPath* iPathNew = nil;
        iPathNew = objc_getAssociatedObject(sender, SMALL_BTN_REF_1);
        (iPath)?(associateIndex = iPath.row):(associateIndex = iPathNew.row);
        UITextField* tempField = nil;
        tempField = objc_getAssociatedObject(sender, LARGE_BTN_REF);
        UITextField* tempMField = nil;
        tempMField = objc_getAssociatedObject(sender, CELL_TO_CELL_REF);
        if(tempField)
        {
            tempTxtField = tempField;
            tempMField = nil;
        }
        else
        {
            tempTxtField = tempMField;
            tempField = nil;
        }
        [popTbl setFrame:[(NatolliAppDelegate*)[[UIApplication sharedApplication] delegate] GetTheCordinateFromTextFieldDynamically:tempTxtField WithButton:sender isAssociativeRefAvailable:YES IfAvailableThenIndex:associateIndex]];
        [popTbl isHidden]?[popTbl setHidden:NO]:[popTbl setHidden:YES];
        [popTbl setHidden:NO];

        switch (iPath.row || iPathNew.row)
        {
            case 0:
                tempOfArr = quantArr;
                [popTbl reloadData];
                break;
            case 1:
                tempOfArr = conditionArr;
                [popTbl reloadData];
                break;
            default:
                break;
        }
    }

忙しくてコードの 1 つを貼り付けただけなので、必要に応じてエアコンを操作する必要があることに注意してください。

ご不明な点がございましたら、お気軽にお問い合わせください。

于 2012-12-20T12:02:55.527 に答える