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;
}
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 的内容。

还有一件事通常是我们为此目的使用标签的习惯,但是我们手里有苹果提供的东西来解决这样的事情,那就是协会。在这里,您应该尝试:-

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

请注意,您需要根据您的要求进行空调工作,因为由于我的忙碌,我只是粘贴了我的一个代码。

如有任何疑问,请回复我。

于 2012-12-20T12:02:55.527 回答