Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7511203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:28:34+00:00 2026-05-29T23:28:34+00:00

I need help figuring out how to display the contents of an NSArray into

  • 0

I need help figuring out how to display the contents of an NSArray into a NSTableView. My NSArray is filled with (or at least I think it is) filenames from a directory. I use NSFileManager to get the names of files in a directory and then I load that info into a NSArray. But I can’t figure out how to load the NSArray into the NSTableView.

AppDelegate.h

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet NSTableView *tableView;

NSArray *list;
IBOutlet NSTextField *text;

NSFileManager *manager;
NSString *path;
NSString *pathFinal;
}

@property (assign) IBOutlet NSWindow *window;

- (IBAction)listArray:(id)sender;

@end

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;

- (int)numberOfRowsInTableView:(NSTableView *)tableView
{
    return [list count];
}

- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn
        row:(int)row
{
    return [list objectAtIndex:row];
}

- (IBAction)listArray:(id)sender {
    path = @"~/Library/Application Support/minecraft/bin/";
    pathFinal = [path stringByExpandingTildeInPath];
    list = [manager directoryContentsAtPath:pathFinal];

    [tableView reloadData];
}

- (void)dealloc
{
    [super dealloc];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

}

@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:28:36+00:00Added an answer on May 29, 2026 at 11:28 pm

    There are two ways to do this: Cocoa Bindings using NSArrayController or by implementing the NSTableDataSource protocol in an object and assigning that object as the table view’s datasource.

    It looks like you have already half-implemented the NSTableViewDataSource method. You need to add the protocol declaration to your interface to indicate that your AppDelegate object implements the protocol:

    @interface AppDelegate : NSObject <NSApplicationDelegate, NSTableViewDataSource> 
    

    You have already implemented the required datasource methods, so in theory everything should be working, but you possibly have not set your AppDelegate object as the table view’s datasource. You can do this in code:

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        [tableView setDatasource:self];
    }
    

    Alternatively, you can assign the datasource in Interface Builder by setting the table view’s datasource outlet to your AppDelegate instance.

    However, the main problem you have is that you are assigning an autoreleased object to your list ivar, and it is being released before the table view reloads.

    Your listArray method is problematic. There is no reason for path and pathFinal to be instance variables. They are only used once, so should be locally scoped. In fact, since path is a constant, it should be declared separately:

    //this should go in the top of your .m file, after the #import directives
    static NSString* minecraftPath = @"~/Library/Application Support/minecraft/bin/";
    
    - (IBAction)listArray:(id)sender 
    {
        NSString* path = [minecraftPath stringByExpandingTildeInPath];
    
        //you want to hang onto the array that is returned here, so you must retain it
        //however, if you don't release the existing value, it will be leaked
        [list release];
        list = nil;
        list = [[manager directoryContentsAtPath:pathFinal] retain];
        [tableView reloadData];
    }
    
    - (void)dealloc
    {
        //because you retained it, you must release it
        [list release];
        [super dealloc];
    }
    

    A much better way to do this would be to declare list as a property and synthesize its accessors:

    .h:

    @interface AppDelegate : NSObject <NSApplicationDelegate, NSTableViewDataSource> {
     ...
    }
    ...
    @property (retain) NSArray* list;
    ...
    

    .m:

    @implementation AppDelegate
    @synthesize list;
    ...
    

    You can then use the property and it handles the retain/release for you:

    - (IBAction)listArray:(id)sender 
    {
        NSString* path = [minecraftPath stringByExpandingTildeInPath];
    
        //you've set the property to use retain, so the synthesized accessor does that for you
        self.list = [manager directoryContentsAtPath:pathFinal];
        [tableView reloadData];
    }
    
    - (void)dealloc
    {
        //you still need to release when done
        self.list = nil;
        [super dealloc];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help figuring out how to convert data that comes in from a
Need help figuring out how to extract text from context (Honda from str), need
Need help figuring out a couple common workflows with Github. I come from a
I need help figuring out how to change out the view in my application.
Hey all, i am in need of some help with figuring out how to
I need help figuring out how to create a cell like the one representing
I need help figuring out why my rspec tests are running slowly. I am
I need help figuring out some regular expressions. I'm running the dig command and
I need help figuring out why I am getting a segmentation fault here. I
I need some help figuring out a solution to a z-index problem with ie7.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.