Resolved UICollectionView and UIMenuController [workaround]

Discussion in 'iOS Programming' started by ChristianJapan, Aug 22, 2013.

  1. ChristianJapan, Aug 22, 2013
    Last edited: Sep 29, 2013

    ChristianJapan macrumors 601

    ChristianJapan

    Joined:
    May 10, 2010
    Location:
    日本
    #1
    I managed to get the UICollectionView running with UIMenuController. In many cases it works perfectly and I get the context menu for the item I selected.
    But in 20% of the cases the item selected and where the menus shows up are be different ...

    Like this case: I clicked on the top left item but the popup comes down left.

    image.jpg

    When looking into indexPath it even shows section 1, item 0; wrongly as I pressed section 0, item 0; can happen with all item.
    Any idea what can be wrong ?
     
  2. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #2
    You're going to need to post some code for us to look at. I would think that the -didSelectItemAtIndexPath: method would be valuable to look at
     
  3. ChristianJapan thread starter macrumors 601

    ChristianJapan

    Joined:
    May 10, 2010
    Location:
    日本
    #3
    sure; hope it helps

    Basic setup is:
    Each section represent one instance of a "connection" (basically host information); one connection can contain several "units"; adressed via .item

    The following functions contain quite some redundant code; I know I should put them in one method but that is more a style/cosmetic question.

    This function actually try to store which "unit" got selected by the user

    Code:
    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    {
       FAHMAppDelegate *app = (FAHMAppDelegate *) [[UIApplication sharedApplication] delegate];
       
       self.actionUnit = nil;
    
       if (indexPath.section >= 0)
       {
          FAHMConnection *connection = [app.document.clientList objectAtIndex:indexPath.section];
          FAHMUnit *unit = [connection.slotList objectAtIndex:indexPath.item];
          self.actionUnit = unit;
       }
       
    }
    
    similar function ... this one update the context menu with respect to one entry; the NSLog in here is actually the one I saw with the wrong information e.g. clicked on section 0, item 0 but get section 1, item 0.

    Code:
    - (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath
    {
       
       FAHMAppDelegate *app = (FAHMAppDelegate *) [[UIApplication sharedApplication] delegate];
       
    #if 1
       NSLog(@"can show action for section %d, item %d ", indexPath.section, indexPath.item);
    #endif
       
       self.actionUnit = nil;
       
       FAHMConnection *connection = nil;
       FAHMUnit *unit = nil;
       
       if (indexPath.section >= 0 && indexPath.section < [app.document.clientList count])
       {
          connection = [app.document.clientList objectAtIndex:indexPath.section];
          unit = [connection getUnitAtIndex:indexPath.item];
          
          self.slotProject.title = [NSString stringWithFormat:@"Project %@", unit.project.project ];
    
          [[UIMenuController sharedMenuController] update];
       }
    
       return YES;
    }
    
    and the "decider code" for what entry in the menu should be shown
    Code:
    - (BOOL)collectionView:(UICollectionView *)collectionView
          canPerformAction:(SEL)action
        forItemAtIndexPath:(NSIndexPath *)indexPath
                withSender:(id)sender
    {
       FAHMAppDelegate *app = (FAHMAppDelegate *) [[UIApplication sharedApplication] delegate];
    
    #if 1
       NSLog(@"can perform action for section %d, item %d ", indexPath.section, indexPath.item);
    #endif
    
       self.actionUnit = nil;
    
       FAHMConnection *connection = nil;
       FAHMUnit *unit = nil;
       
       if (indexPath.section >= 0 && indexPath.section < [app.document.clientList count])
       {
          connection = [app.document.clientList objectAtIndex:indexPath.section];
          unit = [connection getUnitAtIndex:indexPath.item];
       }
       
       if (action == @selector(pauseSlot:))
       {
          if (connection && unit)
          {
             if (!unit.unitIsDummy)
             {
                self.actionUnit = unit;
                return YES;
             }
          }
       }
    ....
    
    
    Some guts feeling tells me it is a problem with the firstResponder; which one should it be: the collectionViewController or the collectionView ?

    Code:
    - (void)viewDidAppear:(BOOL)animated
    
    {
    ...   
       NSLog(@"become first responder");
       [self.collectionView becomeFirstResponder];
    ...
    }
    

    let me know if you need other snipplets.
     
  4. ChristianJapan thread starter macrumors 601

    ChristianJapan

    Joined:
    May 10, 2010
    Location:
    日本
    #4
    ok; I gave up on that one and implemented my own Popover view triggered by a long-tap-gesture ... that way I even have some nicer way to utilize the popover for other stuff. It works flawless (for what I see until now)
     

Share This Page