objective c - NSTableView sort bindings - programatic Solution -


first-time questioner here on site. have been attempting learn more nstableview bindings relation nsarraycontroller taking bindings out of storyboard , trying reproduce effect in code. able configure initial sort order of nstableview , nsarraycontroller using bindings still cannot seem columns sort when clicked.

background have standard, cell-based nstableview in storyboard bound nsviewcontroller through iboutlet. nsviewcontroller has following viewdidload method:

- (void)viewdidload {     [super viewdidload];      //setup array controller     self.arraycontroller = [nsarraycontroller new];     [self.arraycontroller addobject:@{@"namef": @"test1", @"namel": @"a"}];     [self.arraycontroller addobject:@{@"namef": @"test2", @"namel": @"b"}];     [self.arraycontroller addobject:@{@"namef": @"test3", @"namel": @"c"}];     [self.arraycontroller addobject:@{@"namef": @"test4", @"namel": @"b"}];      //setup table view column bindings , sorting     [[self.tableview tablecolumnwithidentifier:@"0"] bind:nsvaluebinding toobject:self.arraycontroller withkeypath:@"arrangedobjects.namef" options:nil];     [[self.tableview tablecolumnwithidentifier:@"0"] setsortdescriptorprototype:[nssortdescriptor sortdescriptorwithkey:@"namef" ascending:yes selector:@selector(caseinsensitivecompare:)]];      [[self.tableview tablecolumnwithidentifier:@"1"] bind:nsvaluebinding toobject:self.arraycontroller withkeypath:@"arrangedobjects.namel" options:nil];     [[self.tableview tablecolumnwithidentifier:@"1"] setsortdescriptorprototype:[nssortdescriptor sortdescriptorwithkey:@"namel" ascending:yes selector:@selector(caseinsensitivecompare:)]];      //bind array controller tableview     [self.tableview bind:nscontentbinding toobject:self.arraycontroller withkeypath:@"arrangedobjects" options:nil];     [self.tableview bind:nsselectionindexesbinding toobject:self.arraycontroller withkeypath:@"selectionindexes" options:nil];      //setup sorting     [self.tableview setsortdescriptors:[nsarray arraywithobject: [[nssortdescriptor alloc] initwithkey:@"namel" ascending:yes selector:@selector(caseinsensitivecompare:)]]];     [self.arraycontroller bind:nssortdescriptorsbinding toobject:self withkeypath:@"sortdescriptors" options:nil];     [self.arraycontroller setautomaticallyrearrangesobjects:yes]; } 

this results in successful loaded screen table view sorted "namel" data , each column header showing clickable arrows aiming up/down each click.

however, sort order not changing...

reading various other articles , stackoverflow questions seeing answers "you need bind columns array controller , nstableview auto-bind itself" , various other explanations regularly involve storyboard.

i've tried various combinations of commenting out components of below code. i've tried changing text inside of sortdescriptorprototypes in each column.

i know i'm missing 1 important clue , documentation on horrible. can see i'm doing wrong? how bind correctly clicking on column headers sorts data?

so common these things found answer myself. come after, read below:

my problem had connected many bindings between nsarraycontroller , nstableview. bindings in question :

a) 2 table-view columns binding values contents of array-controller

b) table-view binding it's contents array-controller

c) table-view binding it's selection index array-controller

d) array controller binding it's sort descriptors local reference of table-view sort descriptors.

i removed bindings b , c , started working. thought had tried before there must have been additional bug when tried then. below functional copy of viewdidload method:

- (void)viewdidload {     [super viewdidload];      //setup array controller     self.arraycontroller = [nsarraycontroller new];      [self.arraycontroller addobject:@{@"namef": @"test1", @"namel": @"a"}];     [self.arraycontroller addobject:@{@"namef": @"test2", @"namel": @"b"}];     [self.arraycontroller addobject:@{@"namef": @"test3", @"namel": @"c"}];     [self.arraycontroller addobject:@{@"namef": @"test4", @"namel": @"b"}];      //setup table view     [[self.tableview tablecolumnwithidentifier:@"0"] bind:nsvaluebinding toobject:self.arraycontroller withkeypath:@"arrangedobjects.namef" options:nil];     [[self.tableview tablecolumnwithidentifier:@"0"] setsortdescriptorprototype:[nssortdescriptor sortdescriptorwithkey:@"namef" ascending:yes selector:@selector(caseinsensitivecompare:)]];      [[self.tableview tablecolumnwithidentifier:@"1"] bind:nsvaluebinding toobject:self.arraycontroller withkeypath:@"arrangedobjects.namel" options:nil];     [[self.tableview tablecolumnwithidentifier:@"1"] setsortdescriptorprototype:[nssortdescriptor sortdescriptorwithkey:@"namel" ascending:yes selector:@selector(caseinsensitivecompare:)]];      //setup sorting     [self.tableview setsortdescriptors:[nsarray arraywithobject: [[nssortdescriptor alloc] initwithkey:@"namel" ascending:yes selector:@selector(caseinsensitivecompare:)]]];     [self.arraycontroller bind:nssortdescriptorsbinding toobject:self.tableview withkeypath:@"sortdescriptors" options:nil];     [self.arraycontroller setautomaticallyrearrangesobjects:yes];  } 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -