0

我正在构建一个使用 UICollectionView 来显示一系列博客文章的应用程序。

当用户点击帖子时,会推送一个 DetailView 以显示帖子的内容。

在详细视图中,可以看到帖子图片、文本等。还有一个按钮可以显示评论。

我希望用户能够点击comments按钮并加载一个 UITableView,它将显示为该帖子编写的所有评论。这是我无法实现的部分。

我用界面生成器创建了一个 UITableView 并使用 segue 将它连接到 DetailView 。点击按钮时comments,我得到一个空表。

在我的 DetailView 上点击评论按钮会触发:

-(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?
    }
}

任何想法如何完成这项工作?很高兴发布更多代码。

这是我的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);
}

CommentViewController.h

#import <UIKit/UIKit.h>

@interface CommentViewController : UITableViewController {
    NSArray *commentArray;
}

@property (strong, nonatomic) id commentArray;

@end
4

1 回答 1

2

你为你的 UITableView 创建了一个控制器吗?

如果这似乎是一个基本答案,我可能没有正确理解您的设计目标,很抱歉,但是如果您正在执行序列,那么您应该初始化 tableview 控制器并以某种方式设置您的数据源。

例如,在你准备 segue 时,你应该有这样的东西:

CommentsControllerView *myTableView = segue.destinationViewController;
myTableView.commentsArray = self.commentsArray;
myTableView.itemId = self.itemId;

在您的自定义表格视图控制器中,您可以创建一个 NSArray 属性来保存评论数组并按照标准程序设置您的表格视图。或者您将使用一些逻辑来检索适当的注释并为您的新表格视图加载表格视图数据源。当它初始化时,它将包含您传递给它的数据,然后它应该像使用 tableview 委托和数据源方法的标准 tableview 一样运行。

这有帮助吗?希望如此。

于 2012-11-23T03:44:24.400 回答