2012-08-17 51 views
0

首先,我想从我开始100%全新的iPhone开发。我相信对于有经验的人来说这是一个非常简单的问题。无论如何,我想要做的是:为什么我的tableView不显示对象?

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

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

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

检查self.searchResults数组数 – Tendulkar 2012-08-17 05:36:23

回答

0

第一步。由于您的TableView是一个IBOutlet,因此请检查您的tableView数据源并为该视图控制器的.xib文件中的委托映射。我的疑问是钩子不正确。

Step2。如果文件的.xib钩UPS是正确的,那么你应该做

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

.... 
} 

,并检查的NSLog正在记录SearchResult所阵列。如果由于某种原因它是空的,你的numberOfRowsInSection委托方法将返回0,因此你的tableView将是空的。

希望这会有所帮助。

在第一行

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

+0

或者您可以通过编码设置delegate和dataSouce。只需在viewDidLoad方法中尝试下面的代码 - self.table.delegate = self; self.table.dataSource = self; – Nayan 2012-08-17 05:48:52

+0

它不工作.....好像dataSource/delegate不是问题。我一定错过了代码中的一些基本内容? tableView可以处理2个名字的对象吗?或者我必须告诉tableView我的对象有2个名字,如Jesper和Martensson,而不仅仅是Jesper? – 2012-08-17 07:56:18

+0

顺便说一句,searchResults数组确实计数... – 2012-08-17 08:03:23

0

与下面码

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

替换此线和连接的tableview委托和数据源的tableview在界面生成器。

希望它有帮助。

+0

仍然没有工作....你认为这可能是一个问题,我的对象有2个项目..?像(项目1)Jesper(项目2)Martensson。它只能显示/接受1件物品吗?我必须考虑的事情? – 2012-08-17 06:34:56

+0

你必须使用NSMutableArray来代替NSArray。可能会解决你的问题。 – Mahendra 2012-08-17 09:13:26

+1

您可以将其显示为标题和副标题。尝试设置cell.textLabel.text = yourObject.name(它是通用的...适应你自己的对象) – 2012-08-17 12:35:52