iphone - 如何从 DetailView 中推送 UITableView?

标签 iphone objective-c ios uitableview ios5

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

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

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

我希望用户能够点击 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

最佳答案

您是否为您的 UITableView 创建了一个 Controller ?

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

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

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

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

有帮助吗?希望如此。

关于iphone - 如何从 DetailView 中推送 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13522744/

相关文章:

ios - 如何更改 UIButton 标题颜色?

ios - 登记机关答复无效

iphone - 为什么 XCode Core Data 启用模板中的 Core Data 堆栈被视为私有(private)?

ios - 如何在 UITextField 中禁止特殊字符但允许亚洲/中东字符

ios - 如何在ios中使用ASL获取控制台日志?

objective-c - 如何将文档显示为图标网格?

ios - 停止在 UITableViewCell 中重复值

objective-c - UITableViewCell 与延迟 XML 解析

ios - [可能重复]:体系结构i386的 undefined symbol :

iphone app允许背景音乐继续播放