为什么我的tableView不显示对象?

时间:2012-08-17 05:28:10

标签: iphone objective-c search tableview

首先,我想开始,我是100%的iPhone开发新手。我相信对于有经验的人来说这是一个非常简单的问题。无论如何,我想要做的是:

用户应该通过SearchUI能够从我的数据库中搜索对象。如果对象存在,则将其显示在将显示搜索对象的tableView中。我设法从数据库中获取对象,但不将它们实例化到tableview中并显示它们。

老实说,我不知道我做错了什么。所有的帮助将非常感谢,如果可能的话也会有一些解释。在方法- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects下 - 我尝试将对象移动到tableView中,但没有成功。您可以在FirstViewController.m TableView Data Scource方法的末尾找到pragma mark中的方法。这是我的代码:

FirstViewController.h类

#import <UIKit/UIKit.h>
#import <RestKit/RestKit.h>

@interface FirstViewController : UIViewController <UITableViewDataSource,           RKObjectLoaderDelegate>
{ 
UISearchDisplayController *searchDisplayController;
UISearchDisplayController *searchBar;
UITableView *table;
NSArray *allItems;
NSArray *searchResults;

}

@property (nonatomic, retain) IBOutlet UISearchDisplayController     *searchDisplayController;
@property (nonatomic, retain) IBOutlet UISearchDisplayController *searchBar;
@property (nonatomic, retain) IBOutlet UITableView *table;
@property (nonatomic, copy) NSArray *allItems;
@property (nonatomic, copy) NSArray *searchResults;
@end

FirstViewController.m类

#import "FirstViewController.h"
#import "Task.h"

interface FirstViewController ()

end

implementation FirstViewController

@synthesize searchDisplayController;
@synthesize searchBar;
@synthesize allItems;
@synthesize searchResults;
@synthesize table;

#pragma mark - View lifecycle

- (void)viewDidLoad
{
//self.listContent = [[NSArray alloc] initWithObjects:@"John", @"Paul", nil];

//self.filteredListContent = [NSMutableArray arrayWithCapacity:10];

[super viewDidLoad];    // Do any additional setup after loading the view, typically  from a nib.
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
else
{
    return YES;
}
}


-(BOOL)searchDisplayController:(UISearchDisplayController *)controller   shouldReloadTableForSearchString:(NSString *)searchString
{
return NO;
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller  shouldReloadTableForSearchScope:(NSInteger)searchOption
{
return NO;
}


- (void)searchBarSearchButtonClicked:(UISearchBar *)pSearchBar
{
[[RKObjectManager sharedManager].mappingProvider setMapping:[Task getMapping]  forKeyPath:@"tasks"];

NSString *path = [NSString stringWithFormat:@"%@/%@/%@", @"/book/1/tasks/", pSearchBar.text, @".json"];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:path delegate:self];
NSLog(@"Search: %@", pSearchBar.text);  
}

#pragma mark - TableView Data Scource methods

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;

cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];

if(cell == nil)
{
    cell =  [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:@"MyCell"];
}

cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
return cell;
}

- (void) deselect { 
//[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow]  animated:YES];
 }

// Respond to user selection tap by coloring the navigation bar
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath  *)newIndexPath
{

}


- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
self.searchResults = objects;

[self.table reloadData];


for(Task *task in objects)
{
    if ([task isKindOfClass:[Task class]])
    {
        NSLog(@"Loaded Book ID: %@ ; Name: %@ ; Book: %@", task.id, task.name,  task.book.name);  
    }
}

}

- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error
{
NSLog(@"Encountered an error: %@", error);  
}
@end

2 个答案:

答案 0 :(得分:0)

步骤1。由于TableView是一个IBOutlet,请检查tableView数据源并委托此视图控制器的.xib文件中的映射。我怀疑是挂钩是不正确的。

第二步。如果.xib文件中的挂钩是正确的,那么你应该

- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects
{
   self.searchResults = objects;
   NSLog(@"%@", searchResults)
   [self.table reloadData];

....
}

并检查NSLog是否记录了searchResults数组。如果由于某种原因它是空的,那么你的numberOfRowsInSection委托方法将返回0,因此你的tableView将为空。

希望这有帮助。

答案 1 :(得分:0)

在第一行

@interface FirstViewController : UIViewController <UITableViewDataSource,           RKObjectLoaderDelegate>
 { 
UISearchDisplayController *searchDisplayController;
UISearchDisplayController *searchBar;
UITableView *table;
NSArray *allItems;
NSArray *searchResults;
}

用以下代码替换此行

@interface FirstViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, RKObjectLoaderDelegate>
 { 
UISearchDisplayController *searchDisplayController;
UISearchDisplayController *searchBar;
IBOutlet UITableView *table;
NSArray *allItems;
NSArray *searchResults;
}

并在接口构建器中连接tableview委托和tableview数据源。

希望它有所帮助。