我正试图在xcode上创建一个ios应用程序 - http://www.appcoda.com/search-bar-tutorial-ios7/。当我输入搜索栏时,一切都在控制台中工作,但输出没有显示在屏幕上
@interface AllUseraStatusViewController ()
@property (weak, nonatomic) IBOutlet UITableView *statusText;
@property (weak, nonatomic) IBOutlet UITableView *statusTime;
@property (weak, nonatomic) IBOutlet UITableView *statusUserName;
@property (strong,nonatomic) NSArray *statusList;
@property (strong,nonatomic) NSMutableArray *allStatusLists;
@property(strong,nonatomic) NSArray *searchResults;
@property(strong,nonatomic) PFObject *object;
@end
@implementation AllUseraStatusViewController
- (void)viewDidLoad {
[super viewDidLoad];
PFQuery *query = [PFQuery queryWithClassName:@"Status"];
[query includeKey:@"user"];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// 3
if (!error){
self.statusList = objects;
[self.tableView reloadData];
} else {
// 4
[[[UIAlertView alloc] initWithTitle:@"Error" message:[error userInfo][@"error"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
}];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView==self.searchDisplayController.searchResultsTableView)
{
return [self.searchResults count];
}else{
return [self.statusList count];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier= @"AllUsersStatusCell";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell==nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView){
self.object =[self.searchResults objectAtIndex:indexPath.item];
NSLog(@"THIS IS object %@",self.object);
}else{
self.object =[self.statusList objectAtIndex:indexPath.item];
}
NSLog(@"THIS IS objectttt %@",self.object); <--this prints correct status in console
UILabel *statusText = (UILabel *)[cell viewWithTag:100];
[statusText setText:[self.object objectForKey:@"status"]];
NSLog(@"THIS IS objectttt %@",statusText);<--but this prints null in console
UILabel *statusTime = (UILabel *)[cell viewWithTag:101];
[statusTime setText:[[self.object createdAt] description]];
UILabel *statusUserName = (UILabel *)[cell viewWithTag:102];
PFUser *userName = [self.object objectForKey:@"user"];
[statusUserName setText:userName.username];
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"detail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UserProfileViewController *vc = segue.destinationViewController;
PFObject *object = [self.statusList objectAtIndex:indexPath.row];
vc.statusObject=object;
}
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForTextSearch:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
-(void)filterContentForTextSearch:(NSString *)searchText scope:(NSString *)scope
{
NSPredicate *resultPredicate=[NSPredicate predicateWithFormat:@"status contains[c] %@",searchText];
NSLog(@"THIS IS %@",resultPredicate);
self.searchResults = [self.statusList filteredArrayUsingPredicate:resultPredicate];
NSLog(@"THIS IS THE %@", self.searchResults);
}
@end
让错误更清晰
这是上面代码中的错误
NSLog(@"THIS IS object %@",self.object);// this prints correct object
UILabel *statusText = (UILabel *)[cell viewWithTag:100];
[statusText setText:[self.object objectForKey:@"status"]];
NSLog(@".....THIS IS objectt %@",statusText.text);// it prints null on console
NSLog(@".....THIS IS objectttt %@",[self.object objectForKey:@"status"]);// this prints correct o/p