質問

While using NSTokenField something strange is happening, as shown in the images below :

As I type A, selection from popup is shown.

As I type A

I scrolled it

enter image description here

Some more scroll, and it went below the visible area.

enter image description here

This is a behaviour with all the tableviews. The view behind the rows are visible , but it automatically springs to normal position. But not in this case.

It is fine in Mail app, it is working fine.

My implementation is :

  1. Created an NSTokenField.

  2. Set its delegate to AppDelegate.

  3. In the implementation file

     -(NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring:(NSString *)substring indexOfToken:(NSInteger)tokenIndex indexOfSelectedItem:(NSInteger *)selectedIndex{
        return @[@"A",@"B",@"C"];
    }
    

Even the sample code from apple documentation behaves incorrectly.

How can I make it auto-spring or restrict by some code?

役に立ちましたか?

解決

What you see in Mail.app is not an actual NSMenu (Apple cheats, shocking!). Turns out, it's actually a custom NSTextField linked to an NSTableView stuck in a transparent window.

F-Scripting FTW

It's a fairly old trick to get around the extremely poor version of scrollWheel: NSMenu happens to have implemented. MTTokenField is a mature alternative to pulling your hair out trying to stick a scroll view in an NSMenu.

他のヒント

You need to predicated the substring with the array content.This will list you the exact matching records(this is the plus point). The other is this will avoid you scrolling as well.

You got to change the delegate method in the following way which will fix the issue.

-(NSArray *)tokenField:(NSTokenField *)tokenField completionsForSubstring:(NSString *)substring indexOfToken:(NSInteger)tokenIndex indexOfSelectedItem:(NSInteger *)selectedIndex
{
    NSArray *arrayContents = @[@"A",@"B",@"C"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[cd] %@", substring];
    return [NSArray arrayWithArray:[arrayContents filteredArrayUsingPredicate:predicate]];
}

Hope this will help you.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top