Displaying data in NSTableView

Discussion in 'Mac Programming' started by titaniumdecoy, Jan 4, 2008.

  1. titaniumdecoy macrumors member

    Oct 13, 2005
    Hi all,

    I want to list the names of the unsaved documents in my application in an NSTableView. Unfortunately it seems I can't just create an array of names and set the table to display it, which would be preferable. My next approach was to instantiate an NSArrayController and set its contentArray to a method -unsavedDocuments which returns the unsaved documents on-the-fly. Unfortunately nothing shows up in the table view when I run the program; I assume this is because I am not using KVC. However, I don't see how I can do that since the documents are stored for me by NSDocumentController. What should I do? Thanks for any suggestions.
  2. titaniumdecoy thread starter macrumors member

    Oct 13, 2005
    I solved one problem only to encounter another. By binding the table column to the arrangedObjects.displayName of an NSArrayController with its contentArray bound to NSDocumentController's documents array (and using a filter predicate to display only unsaved documents), KVC is taken care of for me.

    However, the list of documents is only displayed after I click the table header (to sort the table). I can't figure out how to make them show up otherwise. I tried this, to no avail:

    NSTableView *tableView = [scrollView documentView];
    [tableView reloadData];
    [tableView setNeedsDisplay:YES]
    Any ideas how I can get this working?

    BTW, the following message is displayed only after I click the table header:

    2008-01-04 14:23:09.298 SingleWindowDocBasedApp[2728:10b] KVO autonotifying only supports -set<Key>: methods that return void. Autonotifying will not be done for invocations of -[MyDocument _setDisplayName:].

    EDIT: I got it working by replacing the code above with

    [arrayController rearrangeObjects];
  3. titaniumdecoy thread starter macrumors member

    Oct 13, 2005
    Unfortunately it seems my problem is not yet solved. It turns out that I need an array of tabs (each of which is associated with a document) rather than an array of the documents themselves.

    I have an NSArrayController with its content array bound to the -tabViewItems method of an NSTabView. When I access the arrangedObjects of the array controller, I get the dummy tab that was created in the nib and subsequently removed. I believe this is the same KVC problem I described above.

    How can I bind to the contents of an array which I do not control and is not KVC-compliant?

    (Alternatively, Is there a way to simply load a list of data into a table without using bindings or a data source?)

    EDIT: I found a very hackish workaround, shown below. I hope someone will suggest a better way to do it.

    [tabView willChangeValueForKey:@"tabViewItems"];
    [tabView didChangeValueForKey:@"tabViewItems"];
  4. kainjow Moderator emeritus


    Jun 15, 2000
    Bindings aren't always the best choice. You can use normal NSTableView delegate methods instead of bindings. You simply set your NSTableView's delegate to your controller class, and then implement two methods:

    - (int)numberOfRowsInTableView:(NSTableView *)tableView
        return [myArray count];
    - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)rowIndex
        return [[myArray objectAtIndex:rowIndex] someProperty];
  5. titaniumdecoy thread starter macrumors member

    Oct 13, 2005
    Thanks, kainjow. That is so much simpler than my solution.

Share This Page