2012-10-17 90 views
0

这基本上是我想要的布局:如何将UITableView添加到包含其他对象的父级?

enter image description here

底部应该适应评论到特定的岗位,增加一排为每个评论的UITableView的。

底部的UITableView连接到commentTable;所有其他元素也相应地连线。

当我构建并运行时,没有错误,但我只在帖子下面看到一个空表格单元格。

我知道在将数据加载/传递到我的表中时缺少一些东西,但我想知道是否有人可以给我一个关于如何使这项工作的方向。

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 
+0

你设置/有线委托为tableview?您的委托方法是否被调用(例如,您是否在数据源方法中设置了断点)? – isaac

+0

@isaac对不起,我该怎么做? – pepe

+0

请参阅下面的答案。 – isaac

回答

0

您需要声明您的视图控制器为表视图

改变该行的委托和数据源在您的.h文件

@interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

然后在你的viewDidLoad

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

你也可以看故事板,并以同样的方式连接插座你连接commentTable到你的UITableView,但通过拖动相反的方向,并选择数据源和代表

+0

我的JSON包含帖子和评论正在作为'detailItem'传递 - 所以应该是'commentTable.dataSource = self .detailItem;'而不是? – pepe

+0

啊不,这里有点混乱,当你说'commentTable.dataSource = self'时,你实际上说的是视图控制器正在实现'UITableViewDatasource'协议,并且表视图应该看这个类,并运行这个协议声明的方法(比如'cellForRowAtIndexPath')。然后,您可以像这样完成,告诉表格视图在该方法的每一行中显示的内容。因此,您的.h文件中的更改会宣告您的类正在执行声明为 –

+0

的协议,因为它们现在工作得很好 – pepe

1

这可能是表视图的委托方法的你写不被调用。你应该做的第一件事是在这些方法中设置断点,运行你的应用程序,看看它们是否被调用。

如果他们没有被调用,你可能没有设置你的委托。在这种情况下,您似乎没有使用离散的UITableViewController,而是试图让您的DetailViewController为tableView提供必要的信息以按预期工作。

首先,您需要符合您的DetailViewController到的UITableViewDelegate协议:

@interface DetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 

其次,实际上你需要设置你的UITableView的委托@property。您可以在界面构建器中执行此操作(选择tableview,右键单击并拖动它的委托属性以连接到您的DetailViewController,它可能是也可能不是文件的所有者)。如果你宁愿做在代码中,你只需要调用(早期VC的生活,在viewDidLoad中为例):

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

所以......假设你的委托是所有有线正确,你然后应该返回并测试这些断点以查看是否调用了表视图的方法。如果它们被调用,下一步将是在调用断点时评估变量,例如检查numberOfRowsInSection中返回的数字以及cellForRowAtIndexPath中的值是否符合您的预期。

+0

这些行是否进入'h'或'm' DetailViewController文件? – pepe

+0

第一行在.h中,第二个将在.m – isaac

+0

感谢帮助isaac – pepe

相关问题