User interface 发布将核心数据加载到uitableview的问题

User interface 发布将核心数据加载到uitableview的问题,user-interface,core-data,uitableview,User Interface,Core Data,Uitableview,我已经尝试解决这个问题好几天了,但运气不好,任何建议都将不胜感激 我有一个uitableviewcontroller,可以从核心数据加载数据。加载tableview时,将加载前6个对象(无论实际保存了多少个对象)。当我开始向下滚动时,以下所有单元格都标记为“(null)”。返回顶部时,原始6个单元格中的数据将替换为“(null)”。当我从fetch中记录数组的内容时,会记录核心数据中的所有对象,但当我记录单元格的内容时,会记录前6个内容,并且会记录一长串(null)行 我已经尝试了很多不同的东西

我已经尝试解决这个问题好几天了,但运气不好,任何建议都将不胜感激

我有一个uitableviewcontroller,可以从核心数据加载数据。加载tableview时,将加载前6个对象(无论实际保存了多少个对象)。当我开始向下滚动时,以下所有单元格都标记为“(null)”。返回顶部时,原始6个单元格中的数据将替换为“(null)”。当我从fetch中记录数组的内容时,会记录核心数据中的所有对象,但当我记录单元格的内容时,会记录前6个内容,并且会记录一长串(null)行

我已经尝试了很多不同的东西来调试这个,但是到目前为止没有任何效果

这是tableviewcontroller文件

-(void)viewWillAppear:(BOOL)animated
{
 [self setTitle:@"My Safe"];

self.dictionaryWithContent = [[NSMutableDictionary alloc] init];
self.search = @0;

[self fetchDataFromCoreData];

[self.tableView setFrame:CGRectMake(0, 88, self.tableView.bounds.size.width, self.tableView.bounds.size.height)];

self.searchResults = [[NSMutableArray alloc] init];

self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 44.0f)];

UIBarButtonItem *changeViewsButton = [[UIBarButtonItem alloc] initWithTitle:@"Tweets By User"
                                                                      style:UIBarButtonItemStylePlain
                                                                     target:self
                                                                     action:@selector(switchViewControllers)];

self.navigationItem.rightBarButtonItem = changeViewsButton;

self.tableView.delegate = self;
self.tableView.dataSource = self;

//    CGRect searchView = CGRectMake(0, 44, self.tableView.bounds.size.width, 44);
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
[self.searchController loadViewIfNeeded];
self.tableView.tableHeaderView = self.searchController.searchBar;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.barTintColor = twitter_blue;
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self;

[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:animated];

self.navigationController.navigationBar.barTintColor = twitter_blue;
self.navigationController.navigationBar.translucent = NO;
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
}

-(void)fetchDataFromCoreData
{
AppDelegate *del = [[AppDelegate alloc] init];
NSManagedObjectContext *context = del.managedObjectContext;
NSString *entity;

entity = [NSString stringWithFormat:@"Tweet"];

NSFetchRequest *fet = [NSFetchRequest fetchRequestWithEntityName:entity];

NSError *e;

NSArray *array = [context executeFetchRequest:fet error:&e];
self.arrayWithContent = [[NSArray alloc] initWithArray:array];

for (Tweet *t in self.arrayWithContent) {
    NSLog(@"array %@", t.messageOfTweet);

}

}


-(void)switchViewControllers
{
 PCRSavedTweetByUserTableViewController *vc =        [[PCRSavedTweetByUserTableViewController alloc] init];
PCRNavigationBarController *navBarControllerOfSafeSide = [[PCRNavigationBarController alloc] initWithRootViewController:vc];
[self.navigationController presentViewController:navBarControllerOfSafeSide animated:YES completion:nil];
}

#pragma mark Status bar

- (void)willPresentSearchController:(UISearchController *)searchController {
// do something before the search controller is presented
self.navigationController.navigationBar.translucent = YES;
}

-(void)willDismissSearchController:(UISearchController *)searchController
{
    self.navigationController.navigationBar.translucent = NO;
searchController = self.searchController;

}

    -(void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
searchController = self.searchController;

}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    self.search = @1;

[self.searchResults removeAllObjects];

NSString *searchBarString = self.searchController.searchBar.text;

for (Tweet *tweet in self.arrayWithContent){
    if ([tweet.messageOfTweet containsString:searchBarString] || [tweet.userNameOfTweetUser containsString:searchBarString] || [tweet.whoPosted.name containsString:searchBarString]) {

        [self.searchResults addObject:tweet];
    }
}

[self.tableView reloadData];

}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
 {

[self.searchController.searchBar resignFirstResponder];
self.search = @0;
[self.tableView reloadData];
}

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
self.searchController.searchBar.text = @"";
}

#pragma mark Table

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

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 120.0f;
}

 - (PCRTweetFeedCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
PCRTweetFeedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PCRSavedTweetFeedCell"];
PCRTweet *tweetLocal = [[PCRTweet alloc] init];

if (cell == nil) {
    cell = [[PCRTweetFeedCell alloc] initWithTweet:tweetLocal reuseIdentifier:@"PCRSavedTweetFeedCell"];
}

Tweet *tweetForIndex;

NSArray *arrayForCell = [[self.arrayWithContent reverseObjectEnumerator] allObjects];
NSArray *searchArrayForCell = [[self.searchResults reverseObjectEnumerator] allObjects];

if ([self.search isEqual:@0]){
    tweetForIndex = [arrayForCell objectAtIndex:indexPath.row];
} else if ([self.search isEqual:@1]){
    tweetForIndex = [searchArrayForCell objectAtIndex:indexPath.row];
}

NSString *date = [NSString stringWithFormat:@"%@", tweetForIndex.dateOfTweet];
TweetUser *tweetUser = tweetForIndex.whoPosted;
cell.t = tweetForIndex;

UIImage *imageOfTweetUser;

if (tweetUser.profilePicture) {
    imageOfTweetUser = [UIImage imageWithData:tweetUser.profilePicture];
} else {
    NSURL *urlWithProfilePicture = [NSURL URLWithString:tweetUser.profilePictureURL];
    NSData *dataWithPic = [NSData dataWithContentsOfURL:urlWithProfilePicture];
    imageOfTweetUser = [UIImage imageWithData:dataWithPic];
}

self.imageOfTweetUserGlobal = imageOfTweetUser;

cell.tweetMessage.text = tweetForIndex.messageOfTweet;
cell.tweetDate.text = date;
cell.tweetUserNameLabel.text = tweetForIndex.userNameOfTweetUser;
cell.profilePictureOfTwitterUserImageView.image = imageOfTweetUser;
cell.nameForPassing = [NSString stringWithFormat:@"%@'s Tweet", tweetUser.name];

return cell;
}

也许这是你的问题

如果([self.search isEqual:@0]){

我认为您最初是在获取数据,但一旦初始化searchController,它就会开始查找搜索结果

您正在查找的代码是:


如果([self.search isActive]){

我找到了解决方案。我更改了

AppDelegate *del = [[AppDelegate alloc] init];
NSManagedObjectContext *context = del.managedObjectContext;

而且它工作得很好

AppDelegate *del = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =[del managedObjectContext];