Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-19-2011, 11:57 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 23
rashdan is on a distinguished road
Default UISwitch in a tableview cell...

I've a Switch in a table cell and I want to disable selection on that cell. The problem is that if I do cell.userInteractionEnabled=NO; then it doesn't let me select the Switch, it disables the Switch as well.
And second problem is how can I disable cell highlight. I mean , now when a cell is selected the background of the cell disappears. How can I keep the cell background not to change. Here's wht I'm doing


Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	NSString *MyIdentifier = @"mainMenuIdentifier";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
		[cell setSelectedBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"highlightstrip.png"]]];
        
            UILabel *filterLbl = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 170,20)];
            [filterLbl setFont:[UIFont boldSystemFontOfSize:14.0]];
            [filterLbl setTag:30];
            [filterLbl setBackgroundColor:[UIColor whiteColor]];
            [cell.contentView addSubview:filterLbl];
            [filterLbl release];
            
			if ([[[[collection filterMainList ] objectAtIndex:indexPath.row]  filterItemType] isEqualToString:@"bool"] ) {
				
				UISwitch *mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
				[cell.contentView addSubview:mySwitch];
				cell.userInteractionEnabled = NO;
				cell.accessoryView = mySwitch;
				mySwitch.tag = indexPath.row;
				[mySwitch addTarget:self action:@selector(switchToggled:) forControlEvents: UIControlEventTouchUpInside];
				[mySwitch release]; 
 			}

	}


        [(UILabel *) [cell viewWithTag:30] setText:[[[collection filterMainList ] objectAtIndex:indexPath.row] filterName]];
        
		cell.backgroundColor = [UIColor whiteColor];
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	}
	return cell;
}
Please, tell me how can I resolve both my issues.
rashdan is offline   Reply With Quote
Old 05-19-2011, 01:06 PM   #2 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 8
taylortm is on a distinguished road
Default First problem...

Well, for the first problem, you should use the TableView delegate function

willSelectRowAtIndexPath

and you can use logic to determine the state of your UISwitch for the for the row in question. Based on the switch's status, you can basically disallow the selection of the row in question using this optional delegate method. If you determine you DO NOT want to allow the cell to be selected, you should have willSelectRowAtIndexPath return nil. Otherwise, just return the parameter indexPath.


I haven't dealt with your second problem. I can think about it and take a shot. But I bet you'll find somebody with direct experience on here to help you with that.

Good luck!

Last edited by taylortm; 05-19-2011 at 01:12 PM. Reason: clarify response
taylortm is offline   Reply With Quote
Old 05-20-2011, 02:28 AM   #3 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 23
rashdan is on a distinguished road
Default

Quote:
Originally Posted by taylortm View Post
Well, for the first problem, you should use the TableView delegate function

willSelectRowAtIndexPath

and you can use logic to determine the state of your UISwitch for the for the row in question. Based on the switch's status, you can basically disallow the selection of the row in question using this optional delegate method. If you determine you DO NOT want to allow the cell to be selected, you should have willSelectRowAtIndexPath return nil. Otherwise, just return the parameter indexPath.


I haven't dealt with your second problem. I can think about it and take a shot. But I bet you'll find somebody with direct experience on here to help you with that.

Good luck!
Thanks for the reply, but what I don't understand is, if the Switch is off and I disable the selection, how can I turn the Switch ON if the row selection is disabled. Please, can you provide little code help or something, being new to Obj-C , I didn't totally get what you wrote.
rashdan is offline   Reply With Quote
Old 08-02-2011, 07:22 PM   #4 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 8
taylortm is on a distinguished road
Default do u still care?

Sorry, I hadn't visited this thread for a while. I'm doubting you still have this problem since it's been 2.5 months!

But, just in case you still care...

Quote:
Originally Posted by rashdan View Post
if the Switch is off and I disable the selection, how can I turn the Switch ON if the row selection is disabled.
I should have been more clear: DON'T disable selection via cell.userInteractionEnabled = NO. As you are observing, that turns off the whole cell, including the switch.

So, I'm on a PC at the office and don't have easy access to documentation/code. But, just like you define

Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
you can also define

Code:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
within that (optional delegate) method, you check the value of your switch and decide whether the switch is ON or OFF and either return indexPath or nil.
taylortm is offline   Reply With Quote
Old 09-25-2011, 05:49 PM   #5 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 9
maricello is on a distinguished road
Default

for your first problem:
I think what your looking for is:
Code:
tableView.allowsSelection = NO;
that way the user can't select anything but switches and textViews and anything you put in your TableViewCell should work perfectly normal.
Of course if you need to have only one particular cell to not be selectable, the above proposition with "willSelectRowAtIndexPath:" would be your way to go I think.
greetings!

edit: whow, I just saw how old the thread is. sorry for bringing it up again..

Last edited by maricello; 09-25-2011 at 06:05 PM.
maricello is offline   Reply With Quote
Reply

Bookmarks

Tags
cell selection, tableview, uiswitch

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 357
7 members and 350 guests
doffing81, dre, iOS.Lover, jenniead38, Kirkout, PlutoPrime, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:07 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0