Advertise here




Advertise here

Howdy, Stranger!

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

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

UITableViewCellAccessoryCheckmark - one checkmark at the time

MaticMatic Posts: 14Registered Users
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 Posts: 9,301Tutorial Authors, Registered Users
    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.
  • MaticMatic Posts: 14Registered Users
  • kareem_zokkareem_zok Posts: 34Registered Users
    BrianSlick;107145 said:
    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 Posts: 9,301Tutorial Authors, Registered Users
    BrianSlick;107145 said:
    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;108218 said:
    how can we make it many selection at a time ???
    Try reading the whole post next time.
  • kareem_zokkareem_zok Posts: 34Registered Users
    BrianSlick;108306 said:
    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
    kareem_zok;108619 said:
    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
    Nathan187;170976 said:
    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 Posts: 9,301Tutorial Authors, Registered Users
    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.
Sign In or Register to comment.