Search code examples
objective-cipaduibuttonuilabel

Objective-C: Grid View in iPad issue in removing the object


I have a view where I am showing image and image name in a grid view. As shown in the image.

show image

Now for the same I am taking one UIButton with image as background and UILabel and positioning it accordingly on the screen. Now when I press on the UIButton, it shows the UIActionSheet with option to delete, view and cancel. Now problem comes here. When I press Delete for only one record, the record gets deleted, but if more that one records are there, then gives issues in deleting that it shows the deleted record.

To Add UIButton and UILabel:

-(void)showImageFrame
{
    int FrameWidth = 190;
    int Frameheight = 196;
    int FrameX;
    int FrameY;

    int lblWidth = 190;
    int lblHeight = 40;
    int lblX,lblY;

    settButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [settButton setImage:[UIImage imageNamed:@"Picture frame.png"] forState:UIControlStateNormal];
    [settButton addTarget:self action:@selector(pressSelectedImage:) forControlEvents:UIControlEventTouchDown];

    btnInnerImg = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnInnerImg addTarget:self action:@selector(pressSelectedImage:) forControlEvents:UIControlEventTouchDown];

    lblImgName = [[UILabel alloc]init];
    lblImgName.font = [UIFont boldSystemFontOfSize:30];
    lblImgName.textAlignment = UITextAlignmentCenter;
    lblImgName.textColor = [UIColor whiteColor];
    lblImgName.backgroundColor = [UIColor clearColor];


    for(int i = 0;i<cnt1;i++)
    {
        [settButton setTag:i];
        img = [[appDelegate.DataArray objectAtIndex:i]valueForKey:@"ProfileImage"];

        [btnInnerImg setImage:img forState:UIControlStateNormal];
        [btnInnerImg setTag:i];

        lblImgName.text = [[appDelegate.DataArray objectAtIndex:i]valueForKey:@"strNameImg"];
        [lblImgName setTag:i];

        if(i==0)
        {
            FrameX = 35;FrameY = 119;btnX = 49;btnY=134;lblX = 35;lblY=337;
        }
        if(i==1)
        {
            FrameX = 289;FrameY = 119;btnX = 303;btnY=134;lblX = 289;lblY=337;
        }
        if(i==2)
        {
            FrameX = 545;FrameY = 119;lblX = 545;lblY=337;btnX = 558;btnY=134;
        }
        if(i==3)
        {
            FrameX = 802;FrameY = 119;lblX = 802;lblY=337;btnX = 816;btnY=134;
        }
        if(i==4)
        {
            FrameX = 35;FrameY = 411;lblX = 35;lblY=629;btnX = 49;btnY=426;
        }
        if(i==5)
        {
            FrameX = 289;FrameY = 411;lblX = 289;lblY=629;btnX = 303;btnY=426;
        }
        if(i==6)
        { 
            FrameX = 545;FrameY = 411;lblX = 545;lblY=629;btnX = 558;btnY=426;
        }
        if(i==7)
        {
            FrameX = 802;FrameY = 411;lblX = 802;lblY=629;btnX = 816;btnY=426;
        }

        [settButton setFrame:CGRectMake(FrameX,FrameY,FrameWidth,Frameheight)];
        [lblImgName setFrame:CGRectMake(lblX,lblY,lblWidth,lblHeight)];
        [btnInnerImg setFrame:CGRectMake(btnX,btnY,btnWidth,btnHeight)];

        [newButtonArray addObject:settButton];
        [self.view addSubview: settButton];
        [self.view addSubview: lblImgName];
        [self.view addSubview: btnInnerImg];
    }
}

To remove the UIButton and UILabel:

-(void)removeImageFrame
{
    [settButton removeFromSuperview];
    [btnInnerImg removeFromSuperview];
    [lblImgName removeFromSuperview];

    settButton = nil;
    btnInnerImg = nil;
    lblImgName = nil;        
}

I don't want to use UITableviewCell for the grid view. Also what I am doing is that on deleting 1 record, I am removing all the records and reload them again as per record counter. I know I am doing some mistake in removeImageFrame method, but not able to find it. How can I remove the single UIButton with different Tags?


Solution

  • It would be nice to see a bit more context (the action method and how you get from pressing the button to deleting the button), but here is some advice:

    • Remove all objects from your button array at the start of showImageFrame
    • Create new buttons (with locally declared variables, not an ivar) inside the loop.
    • Don't use a tag of 0, that is the default tag that all views have
    • When a button is tapped, try to pass this as an argument to the removeImageFrame method - that's what sender is for.
    • You could also look at background images and foreground images for a single button instead of having what appears to be one button for the frame and one for a picture
    • (unrelated to your problem) try to think of a better way to display a grid than hard coding coordinates. What it the device is rotated?