2013-09-22 99 views
0

我試圖在UIViewController中創建一個註釋UITableView,但我遇到了問題。如果我直接設置文本,它顯示了(cell.username.text),但如果我試圖用PFQuery(parse.com)沒有填滿它顯示了UITableView無法正常工作

//DetailViewController.h  
    #import <UIKit/UIKit.h> 
    #import <Parse/Parse.h> 

    @interface DetailViewController : UIViewController{ 

    } 

    @property (strong, nonatomic) PFObject *place; 
    @property (strong, nonatomic) IBOutlet PFImageView *userPhoto; 
    @property (strong, nonatomic) IBOutlet UILabel *username; 
    @property (strong, nonatomic) IBOutlet UILabel *message; 
    @property (strong, nonatomic) IBOutlet UILabel *distance; 
    @property (strong, nonatomic) IBOutlet UILabel *checkCount; 
    @property (strong, nonatomic) IBOutlet PFImageView *photo; 
    @property (strong, nonatomic) IBOutlet UIScrollView *scroller; 
    @property (strong, nonatomic) IBOutlet UITableView *commentsTableView; 

    - (IBAction)checkMarkButton:(UIButton *)sender; 


    @end 

//DetailViewController.m 
#import "DetailViewController.h" 
#import "CommentsViewController.h" 

@interface DetailViewController(){ 

    CommentsViewController *test; 
} 

@end 

@implementation DetailViewController 

@synthesize place; 
@synthesize userPhoto, message, username, checkCount, photo, scroller, commentsTableView; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 

    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    test = [[CommentsViewController alloc] init]; 
    self.commentsTableView.delegate = test; 
    self.commentsTableView.dataSource = test; 

    [test commentsQuery:place]; 
} 

//CommentsViewController.h 
#import <Parse/Parse.h> 

@interface CommentsViewController : UITableViewController{ 

} 

@property (strong, nonatomic) PFObject *place; 

- (void)commentsQuery:(PFObject *)object; 

@end 

//CommentsViewController.m 
#import "CommentsViewController.h" 
#import "DetailViewController.h" 
#import "CommentsCell.h" 

@interface CommentsViewController(){ 
    NSArray *commentsArray; 
} 



@end 

@implementation CommentsViewController 

@synthesize place; 



- (void)commentsQuery:(PFObject *)object { 

    place = object; 

    PFQuery *query1 = [PFQuery queryWithClassName:@"activity"]; 
    [query1 whereKey:@"type" equalTo:@"comment"]; 
    [query1 whereKey:@"place" equalTo:place]; 
    [query1 orderByDescending:@"createdAt"]; 
    [query1 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
     if (!error){ 
      commentsArray = [[NSArray alloc]initWithArray:objects]; 
      NSLog(@"%lu", (unsigned long)[commentsArray count]); 
     } 
    }]; 

} 






- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [commentsArray count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    CommentsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    if (cell == nil) { 
     cell = [[CommentsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    PFObject *tempObj = [commentsArray objectAtIndex:indexPath.row]; 

    cell.username.text = @"username"; 
    cell.comment.text = [tempObj objectForKey:@"content"]; 
    cell.userThumbnail.file = [tempObj objectForKey:@"userThumbnail"]; 
    [cell.userThumbnail loadInBackground]; 



    return cell; 
} 
@end 
+0

你確定你從字典中得到的東西不是null?也許你可以NSLog它並更新你的文章的結果? – Albara

+0

我認爲你混合 UITableView IBOutlet(實際上顯示)然後 你自定義 UITableViewController的子類 – tdelepine

+0

@Mr_bem你的權利,commentsQuery在表中出現null,但它工作正常,如果我把nslog在實際的查詢中。我唯一的猜測是我必須刷新表,因爲它在查詢完成前被調用,但是如何刷新表,因爲我沒有訪問[reloadData],因爲IBOutlet UITableView * commentsTableView在DetailViewController中? – david2391

回答