将核心数据加载到uitableview中

时间:2016-03-10 01:21:42

标签: user-interface core-data uitableview

我一直想把这个问题弄清楚几天没有运气,任何建议都会非常感激。

我有一个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;
}

2 个答案:

答案 0 :(得分:0)

好吧也许这是你的问题。

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

我认为你最初是在获取数据,但是一旦你启动了searchController,它就会开始寻找searchResults。

您正在寻找的代码是:

if([self.search isActive]){

答案 1 :(得分:0)

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

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

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

它完美无缺。