need help to insert a button to UIView.

Discussion in 'iOS Programming' started by tranvutuan, Dec 19, 2011.

  1. tranvutuan, Dec 19, 2011
    Last edited by a moderator: Dec 19, 2011

    tranvutuan macrumors member

    Dec 19, 2011
    Iam working on GMGridView and do some customization. Given :
    1. I have a gridView containing gridViewCell
    2. I have button called checkBox
    My Goal is to add checkBox for every gridViewCell
    Below what I have so far
    - (GMGridViewCell *)GMGridView: (GMGridView *)gridView cellForItemAtIndex: (NSInteger)index
        CGSize size = [self sizeForItemsInGMGridView:gridView];
        GMGridViewCell *cell = [gridView dequeueReusableCell];
        if (!cell) 
            cell = [[GMGridViewCell alloc] init];
            // create an UIView
            UIView      *view       =   [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
            // do some customization for my view
            view.backgroundColor = [UIColor purpleColor];
            view.layer.masksToBounds = NO;
            view.layer.cornerRadius = 8;
            view.layer.shadowColor = [UIColor grayColor].CGColor;
            view.layer.shadowOffset = CGSizeMake(5, 5);
            view.layer.shadowPath = [UIBezierPath bezierPathWithRect:view.bounds].CGPath;
            view.layer.shadowRadius = 8;
            // create a button called checkBox
            UIButton    *checkBox   =   [[UIButton alloc] initWithFrame:CGRectMake(70, 20, 150, 25)];
            [checkBox   setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
            [checkBox   setImage:[UIImage imageNamed:@"checkbox_not_ticked"] forState: (UIControlStateNormal)];
                // add checkbox to my view
    [B]        [view addSubView: checkBox]  [/B]
            cell.contentView = view;
    after running it, no checkbox is shown on each cell. I cant see the bug.. Please help if you were experiencing before
    I did attach an picture so you can take a look about it
    ( Again : my goal is to display checkbox on each purple cell)

    Attached Files:

  2. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Well, it looks like you've already highlighted the line that is the problem. Objective-C is case sensitive. You should make sure you don't ignore compiler errors, which that line should be giving you one.
  3. tranvutuan thread starter macrumors member

    Dec 19, 2011
    Thanks for pointing it out. I have just made a change to
    [view addSubview:checkBox];
    It did not work either.. Until I figured it out the problem lies on the line below
    [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    The line was already there after I added a checkBox to my view
    Can you explain because I did not add any subview to my superview which is my cell....
  4. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Was already there? What does that mean? Are you saying Xcode is adding lines of code to your project without your permission?

Share This Page