I’m building an app that uses UICollectionView to display an array of blog posts.
When the user taps on a post, a DetailView is pushed to show the post’s content.
Within the Detail View, one can see a post picture, text etc. There is also a button to show comments.
I want the user to be able to tap on the comments button and load a UITableView that will show all comments written for that post. This is the part I’m unable to implement.
I’ve created a UITableView with interface builder and connected it to the DetailView using a segue. When the comments button is tapped, I get an empty table.
On my DetailView tapping the comments button triggers this:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showComments"]) {
NSDictionary *post = self.detailItem;
NSArray *commentThread = [post objectForKey:@"comment"];
// how do I pass the commentThread to the UITableView at the other end of the segue?
}
}
Any ideas how to get this done? Glad to post more code.
This is my CommentViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"commentCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *comment = [self.commentArray objectAtIndex:indexPath.row];
NSString *commentText = [comment objectForKey:@"comment_text"];
NSString *commentAuthor = [comment objectForKey:@"comment_author"];
cell.textLabel.text = commentText;
return cell;
NSLog(@"%@", comment);
}
And CommentViewController.h
#import <UIKit/UIKit.h>
@interface CommentViewController : UITableViewController {
NSArray *commentArray;
}
@property (strong, nonatomic) id commentArray;
@end
did you create a controller for your UITableView?
I may not be understanding your design goal correctly so sorry if this seems like a basic answer, but if you are performing an seque, then you should initialize the tableview controller and set your datasource in some manner.
for example, in your prepare for segue, you should have something like this:
in your custom tableview controller, you might create a NSArray property to hold the comments array and setup your tableview per standard procedures. Or you would use some logic to retrieve the appropriate comments and load the tableview datasource for your new tableview. When it initializes it will have the data in it that you passed it and then it should operate just like a standard tableview using tableview delegate and datasource methods.
does that help? hope so.