简体   繁体   English

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

[英]Why is my tableView not displaying objects?

First of I would like to start with I am 100% new to iPhone development. 首先,我想从100%接触iPhone开发入手。 I believe this is a quite simple question for someone experienced. 我相信对于有经验的人来说,这是一个非常简单的问题。 Anyhow, what I am trying to do is: 无论如何,我想做的是:

The user should via the SearchUI be able to search for objects from my database. 用户应该通过SearchUI能够从我的数据库中搜索对象。 If the object exist, display it in the tableView where the search-objects will be displayed. 如果对象存在,请在tableView中显示该对象,其中将显示搜索对象。 I manage to get the objects from the database but not instance them into the tableview and display them. 我设法从数据库中获取对象,但没有将它们实例化到tableview中并显示它们。

Honestly I don't know what I am doing wrong. 老实说,我不知道我在做什么错。 All help will be really appreciated and also some explaining if possible. 我们将不胜感激所有帮助,并在可能的情况下提供一些解释。 Under method - (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects - I try to move the objects into the tableView without any success. 在方法下- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects我尝试将对象移动到tableView中,但没有成功。 You find the method in FirstViewController.m at the end of pragma mark TableView Data Scource methods. 您可以在pragma mark TableView Data Scource方法的结尾处​​的FirstViewController.m中找到该方法。 Here is my code: 这是我的代码:

FirstViewController.h class 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 class 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

Step1. 第1步。 Since your TableView is an IBOutlet, check you tableView datasource and delegate mappings in the .xib file for this view controller. 由于TableView是IBOutlet,因此请检查tableView数据源并在.xib文件中为此视图控制器委托映射。 My doubt is that the hooking is not correct. 我的怀疑是挂钩是不正确的。

Step2. 第2步。 If the hook ups in .xib file are correct, then you should do 如果.xib文件中的连接正确,则应这样做

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

....
}

and check if the NSLog is logging the searchResults array. 和检查的NSLog正在记录的SearchResult所阵列。 If that's empty for some reason, your numberOfRowsInSection delegate method will return 0 and hence your tableView will be empty. 如果由于某种原因该字段为空,则您的numberOfRowsInSection委托方法将返回0,因此tableView将为空。

Hope this helps. 希望这可以帮助。

in the first line 在第一行

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

replace this line with below code 用下面的代码替换此行

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

and connect the tableview delegate and tableview datasource in interface builder. 并在接口构建器中连接tableview委托和tableview数据源。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM