samedi 25 avril 2015

Objective C - Remove only UIButtons from superview


I am fairly new to objective-c and I have ran into an issue that I have had a hard time solving..

I am using ShinobiDataGrid and using their prepareCellForDisplay.

To get text to display I would do this:

if([cell.coordinate.column.title isEqualToString:@"Task"]){
            textCell.textField.textAlignment = NSTextAlignmentLeft;
            textCell.textField.text = cellDataObj.task;
        }

but for a particular cell, I am trying to add a custom button:

if([cell.coordinate.column.title isEqualToString:@"selected"]){


            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(0, 10 , 32, 32);



            if(cellDataObj.selected){

                [button setImage:[UIImage imageNamed:@"checked-box.png"] forState:UIControlStateNormal];

            }else{

                [button setImage:[UIImage imageNamed:@"unchecked-box.png"] forState:UIControlStateNormal];

            }

            button.tag = cell.coordinate.row.rowIndex;
            [button addTarget:self action:@selector(CheckBoxPressed:) forControlEvents:UIControlEventTouchUpInside];

            [cell addSubview:button];

            [button removeFromSuperview];

        }

and I first run the app, the checkbox appears. but when I reload my ShinobiDataGrid the checkbox appears again. I tried removing the button from the superview, but that didn't work...any suggestions ? When I reload my ShinobiDataGrid a 3rd time, the checkbox does not appear for a 3rd time, just adds another when I reload the ShinobiDataGrid the 2nd time. Here is the whole method:

- (void)shinobiDataGrid:(ShinobiDataGrid *)grid prepareCellForDisplay:(SDataGridCell *)cell
{

    SDataGridTextCell* textCell = (SDataGridTextCell*)cell;

    CellData *cellDataObj = [cellHolderDisplay objectAtIndex:cell.coordinate.row.rowIndex];

    textCell.textField.textAlignment = NSTextAlignmentCenter;

    if([cell.coordinate.column.title isEqualToString:@"selected"]){


        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 10 , 32, 32);



        if(cellDataObj.selected){

            [button setImage:[UIImage imageNamed:@"checked-box.png"] forState:UIControlStateNormal];

        }else{

            [button setImage:[UIImage imageNamed:@"unchecked-box.png"] forState:UIControlStateNormal];

        }

        button.tag = cell.coordinate.row.rowIndex;
        [button addTarget:self action:@selector(CheckBoxPressed:) forControlEvents:UIControlEventTouchUpInside];

        [cell addSubview:button];

        [button removeFromSuperview];

    }


    if([cell.coordinate.column.title isEqualToString:@"Task"]){
        textCell.textField.textAlignment = NSTextAlignmentLeft;
        textCell.textField.text = cellDataObj.task;
    }

    if([cell.coordinate.column.title isEqualToString:@"BLine. Start"]){
        textCell.textField.text = cellDataObj.baselineDate;
    }
}

Any suggestions ?

Here is the CheckBoxPressed method:

- (void)CheckBoxPressed:(UIButton *)sender
{
    CellData *cell = [cellHolder objectAtIndex:sender.tag];

    if([cell selected] == YES)
    {
        [[cell actualDate]setString:@""];
        [[cell finishedDate] setString:@""];
        [cell setSelected:NO];
    }
    else
    {
        if(([[cell actualDate] isEqualToString:@""]) && ([[cell finishedDate] isEqualToString:@""]))
        {
            [[cell actualDate]setString:[NSString stringWithFormat:@"%@ 8:00:00 AM",[self SetSpecialDateFormat:[NSDate date]]]];
            [[cell finishedDate]setString:[NSString stringWithFormat:@"%@ 4:00:00 PM",[self SetSpecialDateFormat:[NSDate date]]]];
        }
        //actual date not populated but finish date populated
        else if(([[cell actualDate]isEqualToString:@""]) && !([[cell finishedDate] isEqualToString:@""]))
        {
            [[cell actualDate]setString:[cell finishedDate]];
        }
        //finish date not populated but actual date populated
        else if(!([[cell actualDate] isEqualToString:@""]) && ([[cell finishedDate] isEqualToString:@""]))
        {
            [[cell finishedDate]setString:[cell actualDate]];
        }

        [cell setSelected:YES];

    }
    [self UpdateEdittedCells:cell];
    [self SetDisplayHolder];

    //refresh grid
    [gridReference reload];
}


Aucun commentaire:

Enregistrer un commentaire