2012-08-17 48 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