Gridlines controls or objects got scrambled after scrolling


#1

whenever i scrolled my grid line the objects were scattered everywhere but it looks ok when i first viewed it 

firs load

after scrolled

- (void)shinobiDataGrid:(ShinobiDataGrid *)grid prepareCellForDisplay:(SDataGridCell *)cell
    {
        // both columns use a SDataGridTextCell, so we are safe to perform this cast
        SDataGridTextCell* textCell = (SDataGridTextCell*)cell;

           
        
        
        OrderGroup *group=_sortedData[cell.coordinate.row.sectionIndex];
        OrderDetailProduct *order=group.groups[cell.coordinate.row.rowIndex];

        int x=100+cell.coordinate.row.rowIndex;
        
        
        
        // determine which column this cell belongs to
        if ([cell.coordinate.column.title isEqualToString:@"Location"])
        {
            textCell.textField.text = order.Location;
        }
        if ([cell.coordinate.column.title isEqualToString:@"Item No"])
        {
            textCell.textField.text = order.ItemNo;
        }
        if ([cell.coordinate.column.title isEqualToString:@"Product"])
        {
            textCell.textField.text=order.ProducDescription;
          
        }
           if ([cell.coordinate.column.title isEqualToString:@"Temp"])
        {
            
            
            UIImage *imgBtn=[UIImage imageNamed:@"temp_icon20.png"];
            UIImage *imgBtnDisable=[UIImage imageNamed:@"temp_icon_gray20.png"];
            
            UIButton *button =[[UIButton alloc]initWithFrame:CGRectMake(2, 6, imgBtn.size.width, imgBtn.size.height)];
            
            [button setUserInteractionEnabled:YES];
            
            [button setImage:imgBtn forState:UIControlStateNormal];
            [button addTarget:self action:@selector(showTemp:) forControlEvents:UIControlEventTouchUpInside];
            
            UITextField *txtTemp=[[UITextField alloc]init];
                       
            txtTemp.textAlignment=order.Temp;
            txtTemp.delegate=self;
            
            txtTemp.borderStyle=UITextBorderStyleRoundedRect;
            txtTemp.frame=CGRectMake(25, 5, 29, 25);
            txtTemp.textAlignment=UITextAlignmentRight;
            [txtTemp addTarget:self action:nil forControlEvents:UITextFieldTextDidBeginEditingNotification];
            
            
            [textCell addSubview:button];
            [textCell addSubview:txtTemp];

            
        }
        if ([cell.coordinate.column.title isEqualToString:@"Unit"])
        {
            textCell.textField.text =order.Unit;
            
        }
        if ([cell.coordinate.column.title isEqualToString:@"Qty Ordered"])
        {
            textCell.textField.text =[NSString stringWithFormat:@"%@",order.QuantityOrdered];
            
        }
        if ([cell.coordinate.column.title isEqualToString:@"Qty Shipped"])
        {
            textCell.textField.text =[NSString stringWithFormat:@"%@", order.QuanityShipped];
            
        }
        if ([cell.coordinate.column.title isEqualToString:@"Weight"])
        {
            NSNumberFormatter *formatter=[NSNumberFormatter new];
            [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
            float weightFloat=[[NSString stringWithFormat:@"%@",order.Weight]floatValue];
            
            NSString *weightFormatted=[formatter stringFromNumber:[NSNumber numberWithFloat:weightFloat]];
            textCell.textField.text =weightFormatted;
            
        }
        if ([cell.coordinate.column.title isEqualToString:@"Qty Received"])
            
        {
            NSString *strQRcvd=[NSString stringWithFormat:@"%@",order.QuantityReceived];
            UITextField *txtQRecvd=[[UITextField alloc]init];
            if (![strQRcvd isEqualToString:@"(null)"]) {
                
               
                 txtQRecvd.text=order.QuantityReceived;
            }
            else
            {
                txtQRecvd.text=@"0";
               
            }
            txtQRecvd.borderStyle=UITextBorderStyleRoundedRect;
            txtQRecvd.frame=CGRectMake(15, 5, 53, 25);
            txtQRecvd.textAlignment=UITextAlignmentRight;
            txtQRecvd.delegate=self;
            [txtQRecvd addTarget:self action:nil forControlEvents:UITextFieldTextDidBeginEditingNotification];
            [textCell addSubview:txtQRecvd];
            
            
            textCell.textField.keyboardType=UIKeyboardTypeNumberPad;

                    
        }
        if ([cell.coordinate.column.title isEqualToString:@"Reason Code"])
        {
          
                UIImage *imgBtn=[UIImage imageNamed:@"d_Reason_code_Button_Active.png"];
                UIImage *imgBtnDisable=[UIImage imageNamed:@"d_Reason_code_Button_Inactive.png"];
                
                UIButton *button =[[UIButton alloc]initWithFrame:CGRectMake(0, 0, imgBtn.size.width, imgBtn.size.height)];
            
                [button setUserInteractionEnabled:NO];
            
            NSString *strReason=order.ReasonCode;
            
                if (![strReason isEqualToString:@"0"]) {
                    [button setImage:imgBtnDisable forState:UIControlStateNormal];
                }
                else{
                    [button setImage:imgBtn forState:UIControlStateNormal];
                }
            
            UITextField *txtReason=[[UITextField alloc]init];
            if (![strReason isEqualToString:@"(null)"]) {
                
                
                txtReason.text=order.ReasonCode;
            }
            else
            {
                txtReason.text=@"0";
                
            }
            txtReason.borderStyle=UITextBorderStyleRoundedRect;
            txtReason.frame=CGRectMake(50, 5, 35, 25);
            txtReason.textAlignment=UITextAlignmentRight;
            [textCell addSubview:txtReason];

                
                [textCell addSubview:button];
            [textCell addSubview:txtReason];
           
            
        }
        if ([cell.coordinate.column.title isEqualToString:@"Short/Plus"])
        {
            textCell.textField.text =order.ShortPlus;
            
        }
        if ([cell.coordinate.column.title isEqualToString:@"Status"])
        {
                    NSString *strStatus=order.Status;
            UIImage *imgBtn=[UIImage imageNamed:@"d_Green_Check_Mark.png"];
            UIImage *imgBtnDisable=[UIImage imageNamed:@"d_Blank_Circle.png"];
            
            UIButton *button =[[UIButton alloc]initWithFrame:CGRectMake(20, 5, imgBtn.size.width, imgBtn.size.height)];
            [button setUserInteractionEnabled:NO];
            
            if ([strStatus isEqualToString:@"NO"]) {
                [button setImage:imgBtnDisable forState:UIControlStateNormal];
            }
            else{
                [button setImage:imgBtn forState:UIControlStateNormal];
            }
            
            
            
            [textCell addSubview:button];
            

            
        }
        
    }

#2

It’s happening because the cells are being reused and the controls (images/textbox) you’ve added to the cells are still in the cell.

I always clear the cell of text first before I set them and for all other controls, when you add them give them a tag and then remove them if they exist. Use the same tag number for the same type of control in multiple columns.

// both columns use a SDataGridTextCell, so we are safe to perform this cast
        SDataGridTextCell* textCell = (SDataGridTextCell*)cell;

        // By default, clear the text before setting it. I do it this way because I do some validation before setting the cell values.
        // I.E. I only set the textbox value if the object value is not nil (or invalid for what I want to display) otherwise it will 
        // be blank (and this way also not contain the previous text if it was a reused cell).
        textCell.textField.text = @"";

	// Remove the controls from the cell (in case the cell is reused from another column).
	UIButton *button = (UIButton *)[cell viewWithTag:10];
	if (button != nil) [button removeFromSuperview];

         UITextField *txtTemp = (UITextField *)[cell viewWithTag:20];
	if (txtTemp != nil) [txtTemp removeFromSuperview];

        // Continue with rest.
        OrderGroup *group=_sortedData[cell.coordinate.row.sectionIndex];

Wg