Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

UITableViewCellAccessoryCheckmark - one checkmark at the time

MaticMatic Posts: 14Registered Users
edited February 2010 in iPhone SDK Development
Hello,

how can I change the following code (or write new code ) to allow only one checkmark at the time in all sections (there are many sections).
- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *newCell =[tableView cellForRowAtIndexPath:indexPath];
	int newRow = [indexPath row];
	int oldRow = [lastIndexPath row];
	
	if (newRow != oldRow)
		{
			newCell = [tableView  cellForRowAtIndexPath:indexPath];
			newCell.accessoryType = UITableViewCellAccessoryCheckmark;
			
			UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; 
			oldCell.accessoryType = UITableViewCellAccessoryNone;
			
			lastIndexPath = indexPath;	
		}
	
	BusInfoAppDelegate *appDelegate =  
    (BusInfoAppDelegate *)[[UIApplication sharedApplication] delegate];  
    Station *arrivalStationPicked = [[sectionArray
									  objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
	appDelegate.arrivalStationPicked =arrivalStationPicked;
	
	
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	
}
Post edited by Matic on

Replies

  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,407Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited July 2009
    What I do is have a property called "selection" in the view controller. In the case of allowing only a single selection, that property just needs to be of the type of your data model that you are listing. In my case, I'm typically using an NSString, but looks like you might want to use a 'Station' property.

    Then I do something like this in cellForRowAtIndexPath:. In my case I'm putting a custom checked image on the left, but you can do the same thing with your checkmark accessory.
    if ([self selection] != nil)
    {
       if ([[self selection] localizedCaseInsensitiveCompare:currentModelString] == NSOrderedSame)
       {
          [[cell imageView] setImage:[UIImage imageNamed:kCheckedImage]];
          [[cell textLabel] setTextColor:[UIColor blackColor]];
       }
       else
       {
          [[cell imageView] setImage:[UIImage imageNamed:kUncheckedImage]];
          [[cell textLabel] setTextColor:[UIColor grayColor]]; 
       }
    }
    else
    {
       [[cell imageView] setImage:[UIImage imageNamed:kUncheckedImage]];
       [[cell textLabel] setTextColor:[UIColor grayColor]]; 
    }
    

    If there is a selection, figure out if this current item is the same as that selection, and respond accordingly. If there isn't a selection, show nothing.

    Then I do almost the exact same thing in didSelectRowAtIndexPath:
    if ([self selection] != nil)
    {
       if ([[self selection] localizedCaseInsensitiveCompare:currentModelString] == NSOrderedSame)
       {
          [[cell imageView] setImage:[UIImage imageNamed:kUncheckedImage]];
          [[cell textLabel] setTextColor:[UIColor grayColor]];
          [self setSelection:nil];
       }
       else
       {
          [[cell imageView] setImage:[UIImage imageNamed:kCheckedImage]];
          [[cell textLabel] setTextColor:[UIColor blackColor]];
          [self setSelection:currentModelString];
       }
    }
    else
    {
       [[cell imageView] setImage:[UIImage imageNamed:kCheckedImage]];
       [[cell textLabel] setTextColor:[UIColor blackColor]];
       [self setSelection:currentModelString]; 
    }
    
    //...
    
    [tableView reloadData];
    

    Same basic idea. If the item is already selected, turn it off, if not turn it on, etc. Then the reload will take care of resetting the checked status of other items.

    If you want to allow multiple selections, then do the same basic thing, but make your 'selection' property be an array. As items are de/selected, add/remove them to/from the array, then in cellForRow react depending on if your current object is in the selection array.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn - Professional iOS App Development. Available for hire.
    I can provide personalized help at codementor.io

    SlickShopper 2 | Leave a PayPal donation

    Free Xcode Tools: NSLog Utility | Getter Utility
    GitHub Projects: BTIKit | BTICoreDataKit

    Are you a newbie? Things you should read:
    Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
  • MaticMatic Posts: 14Registered Users
    edited July 2009
  • kareem_zokkareem_zok Posts: 34Registered Users
    edited July 2009
    BrianSlick wrote: »
    What I do is have a property called "selection" in the view controller. In the case of allowing only a single selection, that property just needs to be of the type of your data model that you are listing. In my case, I'm typically using an NSString, but looks like you might want to use a 'Station' property.

    Then I do something like this in cellForRowAtIndexPath:. In my case I'm putting a custom checked image on the left, but you can do the same thing with your checkmark accessory.
    if ([self selection] != nil)
    {
       if ([[self selection] localizedCaseInsensitiveCompare:currentModelString] == NSOrderedSame)
       {
          [[cell imageView] setImage:[UIImage imageNamed:kCheckedImage]];
          [[cell textLabel] setTextColor:[UIColor blackColor]];
       }
       else
       {
          [[cell imageView] setImage:[UIImage imageNamed:kUncheckedImage]];
          [[cell textLabel] setTextColor:[UIColor grayColor]]; 
       }
    }
    else
    {
       [[cell imageView] setImage:[UIImage imageNamed:kUncheckedImage]];
       [[cell textLabel] setTextColor:[UIColor grayColor]]; 
    }
    

    If there is a selection, figure out if this current item is the same as that selection, and respond accordingly. If there isn't a selection, show nothing.

    Then I do almost the exact same thing in didSelectRowAtIndexPath:
    if ([self selection] != nil)
    {
       if ([[self selection] localizedCaseInsensitiveCompare:currentModelString] == NSOrderedSame)
       {
          [[cell imageView] setImage:[UIImage imageNamed:kUncheckedImage]];
          [[cell textLabel] setTextColor:[UIColor grayColor]];
          [self setSelection:nil];
       }
       else
       {
          [[cell imageView] setImage:[UIImage imageNamed:kCheckedImage]];
          [[cell textLabel] setTextColor:[UIColor blackColor]];
          [self setSelection:currentModelString];
       }
    }
    else
    {
       [[cell imageView] setImage:[UIImage imageNamed:kCheckedImage]];
       [[cell textLabel] setTextColor:[UIColor blackColor]];
       [self setSelection:currentModelString]; 
    }
    
    //...
    
    [tableView reloadData];
    

    Same basic idea. If the item is already selected, turn it off, if not turn it on, etc. Then the reload will take care of resetting the checked status of other items.

    If you want to allow multiple selections, then do the same basic thing, but make your 'selection' property be an array. As items are de/selected, add/remove them to/from the array, then in cellForRow react depending on if your current object is in the selection array.

    how can we make it many selection at a time ???
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,407Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited July 2009
    BrianSlick wrote: »
    If you want to allow multiple selections, then do the same basic thing, but make your 'selection' property be an array. As items are de/selected, add/remove them to/from the array, then in cellForRow react depending on if your current object is in the selection array.
    kareem_zok wrote: »
    how can we make it many selection at a time ???

    Try reading the whole post next time.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn - Professional iOS App Development. Available for hire.
    I can provide personalized help at codementor.io

    SlickShopper 2 | Leave a PayPal donation

    Free Xcode Tools: NSLog Utility | Getter Utility
    GitHub Projects: BTIKit | BTICoreDataKit

    Are you a newbie? Things you should read:
    Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
  • kareem_zokkareem_zok Posts: 34Registered Users
    edited July 2009
    BrianSlick wrote: »
    Try reading the whole post next time.

    ahh lol ok i didnt notice it before tanks :D:D but what [self selection] do in your code coz mine is almost same but it doesn't check always all items
  • Nathan187Nathan187 Posts: 6New Users
    edited February 2010
    kareem_zok wrote: »
    ahh lol ok i didnt notice it before tanks :D:D but what [self selection] do in your code coz mine is almost same but it doesn't check always all items



    I tried getting this work but it's not quite there. The problem I have is, when I select an item....it doesn't display the checkmark accessory item initially. But when you scroll a lot of items and return to what you selected, then the item is checked. Some how the reload data seems to be messing it up somehow.

    I've tried following Brian's example but I'm think it works in that case since it appears to be a custom cell. This thing has been quite frustrating.

    thanks
  • Nathan187Nathan187 Posts: 6New Users
    edited February 2010
    Nathan187 wrote: »
    I tried getting this work but it's not quite there. The problem I have is, when I select an item....it doesn't display the checkmark accessory item initially. But when you scroll a lot of items and return to what you selected, then the item is checked. Some how the reload data seems to be messing it up somehow.

    I've tried following Brian's example but I'm think it works in that case since it appears to be a custom cell. This thing has been quite frustrating.

    thanks


    Update: I can only get this code to work if my cell is a custom cell. I tried using just a table view with normal/regular cells but the reload event/action always would wipe out the disclosure check. I'm not sure if earlier postings suggested that this code works best for custom table view cells. I'm glad I finally got passed this. It was really pissing me off for a while.

    Thanks for posting the working code.
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,407Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited February 2010
    It's a logic issue, not a cell issue. If you were unable to make it work with standard cells, you were doing something wrong.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn - Professional iOS App Development. Available for hire.
    I can provide personalized help at codementor.io

    SlickShopper 2 | Leave a PayPal donation

    Free Xcode Tools: NSLog Utility | Getter Utility
    GitHub Projects: BTIKit | BTICoreDataKit

    Are you a newbie? Things you should read:
    Definitive Guide To Properties | UITableView Series | A Model (Object) Is A Beautiful Thing
Sign In or Register to comment.