Lookup Fields

Lookup Fields

At the moment, in the NSTableView, your application shows the key of the priority record that indicates the priority of the task. It would be better to see the name of the priority instead of its ID.

You have already ensured that the data from the Priorities table gets downloaded in the DataAccess class’ downloadData method and made that data accessible by adding a prioritiesTable property to the DataAccess class.

Next you need to add a lookup field to the DADataTable object holding the task data. The lookup field will look up the appropriate name of the priority from the priority table based on the priority key value in each task record.

Setting up a lookup field

You need to set up the lookup field in the setupData method of the DataAccess class by calling the addLookupFieldName: sourceField: lookupTable:self.prioritiesTable lookupKeyField: lookupResultField: method on the DADataTable represented by the tasksTable property.

//DataAccess.m
- (void)setupData {
    [self.tasksTable addLookupFieldName:@"PriorityText"
                            sourceField:[self.tasksTable fieldByName:@"Priority"]
                            lookupTable:self.prioritiesTable
                         lookupKeyField:[self.prioritiesTable fieldByName:@"Id"]
                      lookupResultField:[self.prioritiesTable fieldByName:@"Name"]];
}
  • The addLookupFieldName parameter gives your lookup field a name.
  • The sourceField parameter specifies which field in the task record you wish to use as the key for the lookup.
  • The lookupTable parameter specifies which DADataTable object should be used for the lookup, so in this case the DADataTable in the prioritiesTable property.
  • The lookupKeyField parameter specifies which field in the lookupTable should be used as the key for the lookup.
  • The lookupResultField parameter specifies which field in the lookupTable should be used as the result for the lookup.

Using the lookup field in the NSTableView

Now go back to your NSTableView and change the Priority columns binding to use the PriorityText* field rather than the Priority field.

Change Priority Binding

Testing the lookup field

Run your application now. In the Priority column, instead of seeing the value of the property field from the task record, you should now see the appropriate name value from the corresponding record in the Properties table.

Lookup Field in Action