Re: Populating TableView Via Button.
Re: Populating TableView Via Button.
- Subject: Re: Populating TableView Via Button.
- From: "I. Savant" <email@hidden>
- Date: Fri, 18 Sep 2009 09:55:51 -0400
On Sep 18, 2009, at 9:34 AM, Philip Juel Borges wrote:
Does anyone know how you'd populate a tableview when clicking a
button?
Nope. That kind of high-tech stuff is beyond any Cocoa developer. ;-)
I tried this:
-(void)populateTableView:(id)sender {
[super init];
...
return self;
}
This doesn't work. If I replace the void function in the class .m
file with (id) init, everything works fine. But then it populates
the tableview on launch and I'd like to populate the tableView only
when I click a button.
In all seriousness, there are a number of things that seem to be
wrong with your understanding of Objective-C and the Cocoa frameworks.
So first of all, this code example goes beyond trouble populating
the table view. It's as much to do with initializing (what I assume
must be) an instance variable called "sourceArray". Since you said
everything works fine (ie, you see things in your table) when you take
another approach, I'll assume your table datasource (or bindings)
setup is configured correctly.
Second, this very strange call you make to [super init] is
inexplicable. There's just no good reason to call your class's
superclass's init or even the class's own init method from some random
place within your class. Even if you give a good technical reason, I'd
argue you don't. :-) Read the "Allocating and Initializing Objects"
section of the Objective C 2.0 Programming Language guide to get a
better idea of the hows and whys:
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocAllocInit.html#//apple_ref/doc/uid/TP30001163-CH22-SW1
Third, you're returning self within a method that claims to return
nothing at all (void). I assume this and the call to [super init] are
a result of your trying to get things working (and moving them to a
method that you can call yourself, directly). Unfortunately both these
issues will only confuse things further. Move initialization stuff
back to your init method and diagnose the real problem.
Fourth - the real problem: You never ask the table to -reloadData
(if you're using the NSTableDataSource protocol) or you never tell the
NSArrayController where to get its information. So it comes down to
that ... are you using the data source protocol or bindings?
If you're using the data source protocol, all you need to do is
tell the table view to -reloadData when you've changed its data. If
you're using Bindings, it's a bit more complicated: you need to change
your "sourceArray" in a KVO-compliant way so the array controller to
which your table is bound "hears about" the change. Search the
archives for phrases like, "changing the array behind the controller's
back" and read:
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/KeyValueObserving/
(Better yet, create KVC/KVO-compliant accessors for your
sourceArray and use those to set a new array. Using Objective-C 2.0
properties makes this *very* easy.)
--
I.S.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden