|
|
| Welcome to the Mac Forums forums. Please read the FAQ if you have questions. Register to participate. |
|
|||||||
| TouchArcade.com - iPhone Game Reviews and News |
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
|
#1 |
|
macrumors newbie
Join Date: Sep 2009
|
tableview delegate sortDescriptorsDidChange not getting called
I'm trying to sort my tableColumn, but the sortDescriptorsDidChange is not getting called
Three other delegate functions are getting called. Code:
- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex - (id)tableView:(NSTableView * )aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex - (int)numberOfRowsInTableView:(NSTableView *)aTableView All above three get called but the one below does not. I have no idea why. Code:
- (void)tableView:(NSTableView *)tableView sortDescriptorsDidChange:(NSArray *)oldDescriptors Further here is my exact code. Code:
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [employees count];
}
- (id)tableView:(NSTableView * )aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
//What is the identifier for the column?
NSString *identifier = [aTableColumn identifier];
//What person?
Person *person = [employees objectAtIndex:rowIndex];
//What is the value of the attribute named identifier
return [person valueForKey:identifier];
}
- (void)tableView:(NSTableView *)aTableView
setObjectValue:(id)anObject
forTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
NSString *identifier = [aTableColumn identifier];
Person *person = [employees objectAtIndex:rowIndex];
//Set the value for the attribute named identifier
[person setValue:anObject forKey:identifier];
}
- (void)tableView:(NSTableView *)atableView
sortDescriptorsDidChange:(NSArray *)oldDescriptors
{
NSArray *newDescriptors = [atableView sortDescriptors];
[employees sortUsingDescriptors:newDescriptors];
[tableview reloadData];
}
Last edited by BenCoffman : Sep 6, 2009 at 03:10 PM. |
|
|
|
| BenCoffman |
| View Public Profile |
| Find More Posts by BenCoffman |
|
|
#2 | |
|
macrumors 6502
Join Date: Jan 2009
|
Quote:
Try your code with the declaration and definition **exactly** the same. |
|
|
|
|
|
|
#3 |
|
Thread Starter
macrumors newbie
Join Date: Sep 2009
|
|
|
|
|
| BenCoffman |
| View Public Profile |
| Find More Posts by BenCoffman |
|
|
#4 |
|
macrumors newbie
Join Date: Sep 2009
|
tableView:sortDescriptorsDidChange: is a 10.6 only method. Are you certain you're building against the 10.6 libraries?
Also, when are you expecting this to be called? I don't see you setting the sortDescriptor at any point. |
|
|
|
|
|
#5 | |
|
Thread Starter
macrumors newbie
Join Date: Sep 2009
|
Quote:
Upgrading to 10.6 was a good suggestion, though it did not solve my problem. It however made the very annoying unable to read unknown load command 0x80000022 error go away. Unfortunately xcode still does not break in the method during debug and sorting is not working. I think I set the sortDescriptor in the the method above here it is again for convenience. Code:
NSArray *newDescriptors = [aTableView sortDescriptors]; [employees sortUsingDescriptors:newDescriptors]; |
|
|
|
|
| BenCoffman |
| View Public Profile |
| Find More Posts by BenCoffman |
|
|
#6 |
|
macrumors newbie
Join Date: Sep 2009
|
The tableView:sortDescriptorsDidChange: method won't be called unless some other piece of code calls setSortDescriptors: on your tableview.
|
|
|
|
|
|
#7 | ||
|
Thread Starter
macrumors newbie
Join Date: Sep 2009
|
Quote:
Quote:
It does not state that setSortDescriptors needs to be called. I could be wrong though. I won't stand behind my answer.
|
||
|
|
|
| BenCoffman |
| View Public Profile |
| Find More Posts by BenCoffman |
|
|
#8 |
|
macrumors regular
Join Date: Apr 2004
Location: Israel
|
You can also invoke that method by clicking on column headers in the UI to cause a sort to happen.
|
|
|
|
|
|
#9 |
|
macrumors newbie
Join Date: Sep 2009
|
In Cocoa, there are often datasource or delegate methods that contain phrases like "didChange" or "willChange". These methods are called only when the value they're referring to actually changes.
The method we're looking at here, tableView:sortDescriptorsDidChange: is a method of this type. The name of the method implies that it will be called after the sort descriptor for a given tableview changes. To the best of my knowledge, there are two times when this can happen. The first way is when you've used bindings to populate a tableview, then a column header is clicked. That's not what you're doing here. The second way is when setSortDescriptors: is called explicitly. By default, a table has no sortDescriptor. In the code you've provided, a sortDescriptor is never set. Therefore, the sortDescriptor never changes and tableview:sortDescriptorsDidChange: will never be called. At what point in your programs execution do you believe this method should be called? |
|
|
|
|
|
#10 | |
|
Thread Starter
macrumors newbie
Join Date: Sep 2009
|
Quote:
I would like it to be called when I hit the table's column header. I think you are telling me i have not set any criteria for my table column to be sorted, but with setSortDescriptors I can set criteria which in turn will cause my sortDescriptor tableview to get called I can't seem to find any good example on how to use setSortDescriptors? Links or suggestions? |
|
|
|
|
| BenCoffman |
| View Public Profile |
| Find More Posts by BenCoffman |
|
|
#11 | |
|
Demi-God (Moderator)
Join Date: Jun 2000
|
Quote:
Regarding getting this method to work, the way I've always done it is to setup the sort descriptors for each column in IB. Then that method gets called magically and you just resort your array, although I believe an array controller will handle it all if you're using that. |
|
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|