0

This is essentially the layout I want:

enter image description here

The UITableView at the bottom should accomodate comments to a specific post, adding a row for each comment.

The UITableView at the bottom is wired to commentTable; all other elements are wired accordingly as well.

When I build and run, no errors, but I only see one empty table cell below the post.

I know there's something missing in loading/passing data to my table, but I wonder if someone can give me a direction on how to make this work.

DetailViewController.h

#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController {
    IBOutlet UIImageView  *postThumbView;
    IBOutlet UILabel      *postTextLabel;
    IBOutlet UIImageView  *postAuthorPictureView;
    IBOutlet UILabel      *postAuthorNameLabel;
    IBOutlet UILabel      *postTimestampLabel;
    IBOutlet UIScrollView *scroller;
    IBOutlet UITableView  *commentTable;
}

@property (strong, nonatomic) id detailItem;
@end

DetailViewController.m

#import "DetailViewController.h"

@interface DetailViewController ()

- (void)configureView;

@end

@implementation DetailViewController;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self configureView];
}

- (void)configureView
{
    if (self.detailItem) {
        NSDictionary *post           = self.detailItem;
        NSString     *postText       = [post objectForKey:@"post_text"];
        ...

        postTextLabel.text  = postText;
        ...
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *post           = self.detailItem;
    NSDictionary *commentThread  = [post objectForKey:@"comment"];

    return commentThread.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"commentCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSDictionary *post           = self.detailItem;
    NSDictionary *commentThread  = [post objectForKey:@"comment"];

    NSString     *commentText       = [commentThread objectForKey:@"comment_text"];
    NSString     *commentAuthorName = [commentThread objectForKey:@"comment_author_name"];

    cell.textLabel.text = commentText;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"by %@", commentAuthorName];

    return cell;
}

@end
2
  • Have you set/wired the delegate for the tableview? Are your delegate methods being called (eg, have you set breakpoints in the datasource methods)?
    – isaac
    Oct 17, 2012 at 23:48
  • @isaac sorry for my ignorance, where should I do that?
    – pepe
    Oct 17, 2012 at 23:49

2 Answers 2

1

It may be that the table view delegate method's you've written aren't being called. The first thing you should do is set breakpoints inside these methods, run your app, and see if they are being called.

If they're not being called, you may have failed to set your delegate. In this case, it appears that you are not using a discrete UITableViewController, rather you are attempting to have your DetailViewController supply the necessary information for the tableView to work as expected.

First, you need to conform your DetailViewController to the UITableViewDelegate protocol:

@interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

Second, you need to actually set the delegate @property of your UITableView. You can do this in interface builder (select the tableview, right click, drag it's delegate property to connect to your DetailViewController, which may or may not be File's Owner). If you'd rather do it in code, you just need to call (early in the VC's life, in viewDidLoad, for example):

self.tableView.delegate = self;
self.tableView.datasource = self;

So... assuming your delegate is all wired up properly, you should then go back and test those breakpoints to see if the table view's methods are being called. If they are being called, the next step would be to evaluate the variables when the breakpoints are called, examine for example if the numbers being return in numberOfRowsInSection and the values in cellForRowAtIndexPath match what you anticipate.

2
  • do these lines go into the h or m DetailViewController file?
    – pepe
    Oct 18, 2012 at 0:00
  • the first line goes in the .h, the second would be in the .m
    – isaac
    Oct 18, 2012 at 0:01
0

You need to declare your view controller as the delegate and data source for the table view

change this line in your .h file

@interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

Then in your viewDidLoad

commentTable.dataSource = self;
commentTableView.delegate = self;
[commentTableView reloadData];
[self configureView];

You can also look in the story board, and connect the outlets n the same way you connected commentTable to your UITableView, but by dragging in the opposite direction and selecting data source and delegate

10
  • my JSON which contains posts and comments is being passed as detailItem -- so should it be commentTable.dataSource = self.detailItem; instead?
    – pepe
    Oct 18, 2012 at 0:12
  • ah no, a bit of confusion there, when you say commentTable.dataSource = self, what you are in fact saying is that the view controller is implementing the UITableViewDatasource protocol, and that the table view should look at this class, and run the methods declared by this protocol (such as cellForRowAtIndexPath). You then tell the table view what to display in each row in this method, as you have done. Hence the change in your .h file, which is declaring your class as implementing the protocols declared Oct 18, 2012 at 0:16
  • No worries, once you know, you know! Oct 18, 2012 at 0:32
  • There should be no need to call reloadData. Also you stole my answer.
    – isaac
    Oct 18, 2012 at 13:15
  • The call to reloaddata is there in case someone wanted to set the delegate and datasource later on, as it is it will cause no problem. Also, sadly, no, I did not steal your answer, there is more than one person who understands how to set a delegate on a table view. I assume you were also the one who downvoted my answer, very petty. Oct 18, 2012 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.