Checkbox gets hidden over tableview row selection

Discussion in 'iOS Programming' started by AbhishekApple, Sep 10, 2010.

  1. AbhishekApple macrumors member

    Joined:
    Aug 5, 2010
    #1
    I have created custom checkbox(with checkbox_on.jpg and checkbox_off.jpg) and placed it on table view.

    When i select the row the checkbox image gets hidden beneath the highlighted row (selected row) of the table view.

    can ny one suggest
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    As chown33 would say, "we can't debug descriptions". Provide code. If you have errors or warnings, provide those as well. Etc.
     
  3. AbhishekApple thread starter macrumors member

    Joined:
    Aug 5, 2010
    #3
    SearchController.m
    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
    	static NSString *MyIdentifier = @"MyIdentifier";
    	MyIdentifier = @"tblCellView";
    	TableCellViewController *cell = (TableCellViewController *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    	if(cell == nil) {
    		[[NSBundle mainBundle] loadNibNamed:@"TableCellView" owner:self options:nil];
    	}
    	//add checkbox chkItem array of checkbox
    	[cell.contentView addSubview:chkItem[indexPath.row]];
    	// Set up the cell...
    	[cell setProductText:[aryProd objectAtIndex:indexPath.row]];
    	[cell setPriceText:[aryPrice objectAtIndex:indexPath.row]];
    	[cell setProductImage:[aryImg objectAtIndex:indexPath.row]];
    	return cell;
    }
    Checkbox.m

    Code:
    - (id)initWithFrame:(CGRect)frame {
        if (self = [super initWithFrame:frame]) {
            // Initialization code
    		self.contentHorizontalAlignment  = UIControlContentHorizontalAlignmentLeft;
    		
    		[self setImage:[UIImage imageNamed:@"checkbox_not_ticked.png"] forState:UIControlStateNormal];
    		[self addTarget:self action:@selector(checkBoxClicked) forControlEvents:UIControlEventTouchUpInside];
    		
        }
        return self;
    }
    
    
    - (void)drawRect:(CGRect)rect {
        // Drawing code
    }
    
    
    -(IBAction) checkBoxClicked{
    	if(self.isChecked ==FALSE){
    		self.isChecked =TRUE;
    		[self setImage:[UIImage imageNamed:@"checkbox_ticked.png"] forState:UIControlStateNormal];
    		
    	}else{
    		self.isChecked =FALSE;
    		[self setImage:[UIImage imageNamed:@"checkbox_not_ticked.png"] forState:UIControlStateNormal];
    	}
    
    }
    Here are sum Images which will make understand,
    Initially checkbox is clicked if i select the row it still shows the check mark but if i deselect and again select the checkbox it doesn't shows the checkmark..
    [​IMG][​IMG][​IMG]

    I came across one more problem i am unable to select the checkbox of last row as it is hidden behind the toolbar. Even if i scroll up and try to check the last checkbox it automatically scrolls down and 2nd last checkbox gets selected.

    [​IMG]
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    First, assuming TableCellViewController is a subclass of UITableViewCell calling it a ...Controller seems confusing.

    Second, you are loading a nib but not assigning it to anything especially cell.

    Third, why are you adding your checkbox subview here? Since you are already using a custom cell, you should just add it to that.

    Fourth, how is chkItem defined?

    As you can tell, there are a number of issues with even just this small portion of code. Are you sure you understand the basics of table view programming?

    You need to check your layout and make sure your toolbar is not overlapping the tableView but the tableView just comes up to the toolbar's edge.
     
  5. AbhishekApple thread starter macrumors member

    Joined:
    Aug 5, 2010
    #5
    searchController.h
    Code:
    @interface SearchViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{
    	
    	NSMutableArray *arryProduct;
    	NSMutableArray *arryImage;
    	NSMutableArray *arryPrice;	
    [COLOR="red"]IBOutlet TableCellViewController *tblCell;[/COLOR]   //this is connected with TableCellView in IB
    ....
    ....
    }
    ...
    @end
    SearchController.m
    Code:
    #import "SearchViewController.h"
    #import "ASIHTTPRequest.h"
    #import "TableCellViewController.h"
    #import "Checkbox.h"
    @implementation SearchViewController
    [COLOR="red"]Checkbox *chkItem[5];[/COLOR]
    
    - (void)viewDidLoad {
    	self.title = @"Search Product";
        [super viewDidLoad];
    	dispRec = 5;
    	for(int i=0;i<dispRec;i++){		
    	CGRect CheckBoxFrame=CGRectMake(270,27,32,29);
    	[COLOR="red"]chkItem[i] [/COLOR]=[[Checkbox alloc]initWithFrame:CheckBoxFrame];
    	}
    	
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
    	static NSString *MyIdentifier = @"MyIdentifier";
    	MyIdentifier = @"tblCellView";
    	TableCellViewController *cell = (TableCellViewController *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    	if(cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:[COLOR="red"]@"TableCellView[/COLOR]" owner:self options:nil];
    [COLOR="Red"]cell=tblCell;[/COLOR]
    	}
    	//add checkbox chkItem array of checkbox
    	[cell.contentView addSubview:chkItem[indexPath.row]];
    	// Set up the cell...
    	[cell setProductText:[aryProd objectAtIndex:indexPath.row]];
    	[cell setPriceText:[aryPrice objectAtIndex:indexPath.row]];
    	[cell setProductImage:[aryImg objectAtIndex:indexPath.row]];
    	return cell;
    }
    Coz am using the same Custom cell in other tableview where i don't need checkbox. And adding it in custom cell and keeping it hidden in other table view will make overhead of loading the checkbox image.

    Sorry if am slow i have started programming in iphone since last month...:apple:
     
  6. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    Your checkbox is a button subclass? You should set the image for the highlighted state also.

    By the way, this method of adding the checkbox the way you're doing it is overly complicated. You should just add the checkbox in IB or add it once when the nib is loaded, not every time cellForRowAtIndexPath is called.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Alright, so you don't have much experience. Let us know what you have done to educate yourself on the basics of iPhone programming, so we can better tailor our answers to be more helpful. That is, have you read any books, taken any tutorials, watch some videos (perhaps from iTunes U), etc? If so, what? Remember, be specific.
     
  8. AbhishekApple thread starter macrumors member

    Joined:
    Aug 5, 2010
    #8
    First i studied
    Objective c
    http://www.otierney.net/objective-c.html
    xcode--only studied the basics like how to start developing in xcode
    http://developer.apple.com/library/ios/#documentation/Xcode/Conceptual/iphone_development/000-Introduction/introduction.html%23//apple_ref/doc/uid/TP40007959-CH1-SW1
    and also watched some videos in youtube and viemo (don't remember links)
    Later for quick development i started with "Hello world" and other app tutorial
    http://www.iphonesdkarticles.com/search/label/iPhone%20SDK%20Tutorials
    Now using gooogle and forums for other help..
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    First, make sure you have thoroughly read the Table View Programming Guide for iOS.

    Second, is TableCellViewController a subclass of UITableViewCell? If so, I would refactor it to be called CustomCell or something like that.

    Third, I would probably define chkItem as an NSArray or NSMutableArray (and probably call it checkboxItems, as well).

    Fourth, you still aren't assigning your loaded nib to anything. cell=tblCell; is not the solution.

    Fifth, as PhoneyDeveloper already suggested, if you going to add your subview in your code, do it inside the cell == nil block.
     
  10. AbhishekApple thread starter macrumors member

    Joined:
    Aug 5, 2010
    #10
    ???? didn't get this?

    How can i add the custom class of Checkbox (ur right checkbox inherits UIButton) in IB..
    Also i searched for adding checkbox after nib is loaded but couldn't find.!!

    if(cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:mad:"TableCellView" owner:self options:nil];
    for(int i=0;i<dispRec;i++){
    [cell.contentView addSubview:chkItem];
    }
    cell = tblCell;
    }

    I tried with above code but it is not displaying checkbox...their is no compilation or runtime error.
     
  11. AbhishekApple thread starter macrumors member

    Joined:
    Aug 5, 2010
    #11
    I am creating chkItem array so that each instance is bind with each cell of the table which will identify cell details when i want to retrieve the checked item details.

    I am noting getting this part i have got reference from
    http://www.iphonedevsdk.com/forum/iphone-sdk-development/12550-how-load-table-view-cell-nib.html

    For this i hv tried but didn't succeed check abv post
     
  12. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #12
    Code:
    [self setImage:[UIImage imageNamed:@"checkbox_not_ticked.png"] forState:UIControlStateHighlighted];
    
    etc. If you set the image for one state set it for both states.

    BTW, this is correct:

    Code:
    cell = tblCell;
    To create a custom UIButton subclass in IB add a normal button in IB and then change the class of the object to your custom subclass. You will probably need to override initWithCoder: in the subclass to do any initialization.
     
  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    "When I want to retrieve the checked item details"? You should not be using your UI elements as your data store. It should be separate and drive the UI display.

    You'll notice in that reference that they do assign cell to something, namely cell = [self loadCell]; If you are not sure what you are doing and are going to use that code you should probably use it the way it is shown and not make too many changes. Besides, that thread is from 2008. It may not be applicable anymore. But is was posted by Phoney Developer who is probably the same user that is already posting in this very thread. Perhaps they can provide further insight.

    You have an unnecessary for loop inside your block and are now using a new variable: chkCompare. It is hard to follow along if you keep changing the code for no good reason.
     
  14. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #14
    The correct way to load a cell from a nib hasn't changed since 2008. However, when I wrote that example there was no Apple example. Now there is in the tableview programming guide. And I guess that that is the source of OP's code.

    The basic idea is simple. Create an assign IBOutlet for the cell in the view controller. Connect the outlet to the cell in the nib. Load the nib with loadNibNamed: Once the nib is loaded the outlet will be set.

    In the example in that old thread I use a wrapper function to load the nib but it just returns the value of the outlet. If you don't use a wrapper method like that you end up with the code that OP shows, which is correct. Namely it loads the nib and then assigns the outlet to the cell local variable.

    You often see incorrect code that iterates through the topLevelObjects array looking for the view. Use the Outlet.
     
  15. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    Thanks for the clarification, PhoneyDeveloper.
     
  16. AbhishekApple thread starter macrumors member

    Joined:
    Aug 5, 2010
    #16
    Thanks Phoney Developer this worked and also with other post by dejo i discovered and solved other issues too..
    Thanks dejo..
     

Share This Page