Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

AbhishekApple

macrumors member
Original poster
Aug 5, 2010
86
0
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
 

AbhishekApple

macrumors member
Original poster
Aug 5, 2010
86
0
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

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..
Checkbox1.png
Checkbox2.png
Checkbox3.png


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.

checkbox4.png
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
SearchController.m
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	static NSString *MyIdentifier = @"MyIdentifier";
	MyIdentifier = @"tblCellView";
	[COLOR="Red"]TableCellViewController[/COLOR] *cell = (TableCellViewController *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if(cell == nil) {
		[COLOR="Red"][[NSBundle mainBundle] loadNibNamed:@"TableCellView" owner:self options:nil];[/COLOR]
	}
	//add checkbox chkItem array of checkbox
	[cell.contentView [COLOR="red"]addSubview[/COLOR]:[COLOR="Red"]chkItem[/COLOR][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;
}
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?

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.
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.
 

AbhishekApple

macrumors member
Original poster
Aug 5, 2010
86
0
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.

Fourth, how is chkItem defined?

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;
}

Third, why are you adding your checkbox subview here? Since you are already using a custom cell, you should just add it to that.
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.

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?
Sorry if am slow i have started programming in iphone since last month...:apple:
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Sorry if am slow i have started programming in iphone since last month...:apple:
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.
 

AbhishekApple

macrumors member
Original poster
Aug 5, 2010
86
0
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.

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..
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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.
 

AbhishekApple

macrumors member
Original poster
Aug 5, 2010
86
0
Your checkbox is a button subclass? You should set the image for the highlighted state also.
???? didn't get this?

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.

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.
 

AbhishekApple

macrumors member
Original poster
Aug 5, 2010
86
0
Third, I would probably define chkItem as an NSArray or NSMutableArray (and probably call it checkboxItems, as well).

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.

Fourth, you still aren't assigning your loaded nib to anything. cell=tblCell; is not the solution.
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

Fifth, as PhoneyDeveloper already suggested, if you going to add your subview in your code, do it inside the cell == nil block.
For this i hv tried but didn't succeed check abv post
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
???? didn't get this?

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.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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.
"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.

For this i hv tried but didn't succeed check abv post
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.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 

AbhishekApple

macrumors member
Original poster
Aug 5, 2010
86
0
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.

Thanks Phoney Developer this worked and also with other post by dejo i discovered and solved other issues too..
Thanks dejo..
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.