You are here

Retrieving and adding comments to the node detail view

This task shows you how to retrieve comments and then display them in the node detail view.
This tutorial shows you how to retrieve and add comments to the node detail view.

Modifying the header file

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

                  
    #import "AlfrescoCommentService.h"     
                  
                

    This includes the comment service which you will be using to retrieve comments for the selected node.

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

    
    @property (strong, nonatomic) SkyVaultCommentService *commentService;
    
                

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

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

    
    @property (strong, nonatomic) NSArray *comments;
    
                

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

  4. You will also be creating a method to fetch comments, so you could create the prototype for this method now. After the prototype for initWithSession:andNode:, you can add the following:

                  
    - (void)getComments;              
                  
                

    DocumentTableViewController.h will now contain the following code:

                  
    #import <UIKit/UIKit.h>
    
    #import "AlfrescoSession.h"
    #import "AlfrescoNode.h"
    #import "AlfrescoDocumentFolderService.h"
    #import "AlfrescoCommentService.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;
    
    - (id)initWithSession:(id<AlfrescoSession>)createdSession andNode:(AlfrescoNode *)nodeToBeDisplayed;
    - (void)getComments;   
    @end
                  
                  
                
You have prepared the DocumentTableViewController.h header file to support comments.

Adding a method to retrieve comments

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

                  
    #import "AlfrescoCommentService.h"              
    #import "AlfrescoComment.h"              
                  
                

    This includes the comment service and comment classes which you will be using to retrieve comments for the selected node.

  2. Now add the method to retrieve the comments just after the initWithSession:andNode: method:

    
    - (void) getComments
    {
        // get comments for node
        if(nil != self.session && self.node != nil)
        {
            // get the comments using a SkyVaultCommentService
            self.commentService = [[AlfrescoCommentService alloc] initWithSession:self.session];
            __weak DocumentTableViewController *weakSelf = self;
            
            [self.commentService retrieveCommentsForNode:self.node completionBlock:^(NSArray *array, NSError *error){
                    if (nil == array)
                    {
                        NSLog(@"ERROR: No comments array returned: %@", error);
                    }
                    else if (0 < [array count])
                    {
                        weakSelf.comments = [NSArray arrayWithArray:array];
                        [weakSelf.tableView reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];
                    }
                }];
        }
    }
    
                

    This code returns an array of comments via the completion block. As the comments are associated with section 2 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 comments are displayed.

    Note: Using __weak to create a weak reference to self is recommended practice for code inside completion blocks.
You have added the method to retrieve and display comments. 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 comments. Navigate to the method viewDidLoad and add the getComments method call as shown:

                  
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.title = @"Node Details";
    
        [self getComments];
    }              
                
  2. You also need to change the number of rows that will be allocated in the table view for the comments returned. The number of rows in the section will now depend on the number of comments returned. Navigate to the method - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section and change the case statement for the comment section as follows:

    
        case 2: // Comments section
            numRows = [self.comments count]; 
            break;
    
                

    This will cause the number of rows in this section to equal the number of comments retrieved.

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

                  
            // SECTION 2 - Comments
            
            if (indexPath.section == 2)
            {
              SkyVaultComment *comment = [self.comments objectAtIndex:indexPath.row];
              cell.textLabel.text = comment.content;
            }              
                  
                

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

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

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

In this tutorial you added code to retrieve and display comments.