You are here

Retrieving and adding tags to the node detail view

This task shows you how to retrieve tags and then display them in the node detail view.
You will now add code so that tags for a node can be retrieved and displayed in the node detail view.

Modifying the header file DocumentTableViewController.h

You need to modify the header file DocumentTableViewController.h in order to add support for tags.
  1. Load DocumentTableViewController.h into Xcode. You need to include the SkyVault Tagging Service as this will be used to retrieve tags. Add the following import statement:

                  
    #import "AlfrescoTaggingService.h"     
                  
                

    This includes the tagging service which you will be using to retrieve tags for the selected node.

  2. You need a property to store the retrieved SkyVaultTaggingService instance in. Below the other properties in the file add the following:

    
    @property (strong, nonatomic) SkyVaultTaggingService *taggingService;
    
                

    This creates a property that can be used from other code that references the tagging service.

  3. You next need a property to store the retrieved tags in. The tags can be stored in an array. Below the other properties in the file add the following:

    
    @property (strong, nonatomic) NSArray *tags;
    
                

    This creates a property that can be used to reference an array of tags.

  4. You will also be creating a method to fetch tags, so you could create the prototype for this method now. After the prototype for -(void)getComments, you can add the following:

                  
    - (void)getTags;              
                  
                

    DocumentTableViewController.h now contains the following code:

                  
    #import <UIKit/UIKit.h>
    
    #import "AlfrescoSession.h"
    #import "AlfrescoNode.h"
    #import "AlfrescoDocumentFolderService.h"
    #import "AlfrescoCommentService.h" 
    #import "AlfrescoTaggingService.h"
    
    @interface DocumentTableViewController : UITableViewController
    
    @property (strong, nonatomic) SkyVaultNode *node;
    @property (strong, nonatomic) id<AlfrescoSession> session;
    @property (strong, nonatomic) SkyVaultDocumentFolderService *documentFolderService;
    @property (strong, nonatomic) SkyVaultCommentService *commentService;
    @property (strong, nonatomic) NSArray *comments;
    @property (strong, nonatomic) SkyVaultTaggingService *taggingService;
    @property (strong, nonatomic) NSArray *tags;
    
    - (id)initWithSession:(id<AlfrescoSession>)createdSession andNode:(AlfrescoNode *)nodeToBeDisplayed;
    - (void)getComments;
    - (void)getTags;
    
    @end
                  
                  
                
You have now prepared the DocumentTableViewController.h header file to support tags.

Adding a method to retrieve tags

In this task you will add a method to retrieve tags.
  1. Load DocumentTableViewController.m into Xcode. You will need to include a couple of header files, one to include SkyVaultTaggingService and one for SkyVaultTag, as these will be used in the method to retrieve tags:

                  
    #import "AlfrescoTaggingService.h"              
    #import "AlfrescoTag.h"              
                  
                

    This includes the tagging service and tag classes which you will be using to retrieve tags for the selected node.

  2. Now add the method to retrieve the tags just after the - (void) getComments method:

    
    - (void) getTags
    {
        if(nil != self.session && self.node != nil)
        {
            self.taggingService = [[AlfrescoTaggingService alloc] initWithSession:self.session];
            __weak DocumentTableViewController *weakSelf = self;
            [self.taggingService retrieveTagsForNode:self.node completionBlock:^(NSArray *array, NSError *error){
                    if (nil == array)
                    {
                        NSLog(@"ERROR: No tags array returned: %@", error);
                    }
                    else
                    {
                        weakSelf.tags = [NSMutableArray arrayWithArray:array];
                    }
                    [weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:3] withRowAnimation:UITableViewRowAnimationNone];
                }];
        }
    }
    
                

    This code returns an array of tags via the completion block. Using __weak to create a weak reference to self is recommended practice for code inside completion blocks. As the tags are associated with section 3 in the node detail table view, this section of the table view needs to be redrawn when the completion block completes so that the retrieved tags are displayed.

You have added the method to retrieve and display tags. There are however a few more changes that need to be made before this code can be built and run.

Further additions and changes to the code

In this task you will add a few more changes to the code to get a working application.
  1. The first change you need to make is to add code to call the method to retrieve the tags. Navigate to the method viewDidLoad and add the following method call after the call to getComments:

                  
    [self getTags];
                  
                
  2. You also need to change the number of rows that will be allocated in the table view for the tags returned. The number of rows in the section will now depend on the number of tags returned. Navigate to the method - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section and change the case statement for the tags section as follows:

    
        case 3: // Tags section
            numRows = [self.tags count]; 
            break;
    
                

    This will set the number of rows in this section to the number of tags retrieved.

  3. You now need to set the table cells in the tags section to the tag value. Navigate to the tableView:cellForRowAtIndexPath: method. Find the if statement that corresponds to section 3 of the the table view, the tags section. Change the code as follows:

                  
            // SECTION 3 - Tags
            
            if (indexPath.section == 3)
            {
                SkyVaultTag *tag = [self.tags objectAtIndex:indexPath.row];
                cell.textLabel.text = tag.value;
            }
            
                

    This code retrieves the tag from the tags array that corresponds to the row being drawn and then sets the cell's text to be that of the tag's value.

  4. Build and run your code. Your application should show something similar to the following when a node is selected. Note the tags are listed in the tags section.

You have added the final code snippets to ensure a working application.

In this tutorial you added code to retrieve and display tags for a node.