【问题标题】:UITableView doesn't refresh when NSPredicate set设置 NSPredicate 时 UITableView 不刷新
【发布时间】:2015-07-03 14:01:03
【问题描述】:

前言:我试过[self.tableview reloadData]。我的应用程序不会崩溃,但也不会刷新。

我试图在一个TableViewController 上显示两组结果,具体取决于是否设置了NSPredicate。我正在尝试找到一个免费的术语答案,一旦设置了谓词,我如何更新UITableViewController。

我的数据在视图最初加载时正确显示。我的谓词设置正确,我在返回值中抛出的所有日志语句......但是设置时没有任何反应。

我知道这是一个非常愚蠢的问题,但我太笨了。我发现的每个答案都涉及UISearchBar、不推荐使用的方法,或者是答案的 sn-p 等等。

我想展示的内容:

1) 一个显示所有内容的普通 fetchRequest - 这很有效:我的 managedObjectContext 中的所有内容都设置在我的viewDidLoad w/o an NSPredicate 中。

2) 一旦我设置了NSPredicate,我想更新tableView--这不会崩溃,但不会更新tableView

在MyTableViewController 上,我有一个弹出框,当我单击 MyTableViewController 上的 UIBarButtonItem 时会实例化。在弹出框上,我在 MyTableViewController 上设置了一个谓词。

基本上,我想切换显示的内容,并且显示切换取决于我的变量是 nil(所有内容都显示)还是已过滤(变量设置谓词)。

我是第一个尝试这一软件工程壮举的人,也是最愚蠢的人。考虑到我作为最终用户见过多少次,我会赌后者。 如果您知道我做错了什么,我会很高兴收到您的来信。感谢您的阅读。

我有两个实体:PointOfInterest 和 LocationCategory。我有一个UIBarButton 触发一个弹出窗口,其中MyTableViewController 是一个代表。单击弹出框会在MyTableViewController 上设置selectedCategory 属性。:

// This gets set by PopoverTableViewController    
@property (nonatomic, strong) LocationCategory *selectedCategory;     

// This is set on `MyTableViewController`
@property (nonatomic, strong) NSPredicate *searchPredicate;

MyTableViewController 上的属性设置正确。我正在尝试使用selectedCategory 对象作为谓词的基础,以显示“pointOfInterest”对象的“过滤”列表。我从这个TVC 中省略了样板代码,但我在TVC 上有了tableView 委托方法。

我想我需要将我的 fetchedResultsController 设置为 nil 并在我的 refreshTableViewController 方法中使用谓词重新实例化它。它不会崩溃,但也不起作用。

这是MyTableViewController 的viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    self.managedObjectContext = appDelegate.managedObjectContext;

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    NSLog(@"savedPOI managedObjectContext is %@", self.managedObjectContext);

    // Initialize fetch request
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"PointOfInterest"];

    // Add sort descriptors
    [fetchRequest setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]]];

    // Initialize Fetched Results Controller
    self.fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];

    // Configure a fetchedResultsController
    [self.fetchedResultsController setDelegate:self];

    // Perform fetch
    NSError *error = nil;
    [self.fetchedResultsController performFetch:&error];

    if (error) {
        NSLog(@"Unable to perform fetch");
        NSLog(@"%@, %@", error, error.localizedDescription);
    }

    // This sets up an observer that listens for when `selectedCategory` is set
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshTableViewController:) name:@"CategorySelected" object:nil];    
}

一旦我的selectedCategory 被设置,我就会设置以下方法来运行。它不会崩溃,但不会刷新MyTableViewController。我怀疑我这里有一个小错误,但我不确定我做错了什么。

- (void)refreshTableViewController:(NSNotification *)notification {
    if ([[notification name]isEqualToString:@"CategorySelected"]) {
        NSLog(@"NSNotification on SavedPOITableViewController's self.selectedCategory is %@", self.selectedCategory.categoryName);

        // Nuke the cache that was loaded in viewDidLoad
        [NSFetchedResultsController deleteCacheWithName:@"PointOfInterest"];

        // Nuke the fetchedResultsController alloc/inited in viewDidLoad
        self.fetchedResultsController = nil;

        // Alloc init a new fetchRequest
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

        // Set the entity
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"PointOfInterest" inManagedObjectContext:self.managedObjectContext];
        [fetchRequest setEntity:entity];

        // Add sort descriptors
        [fetchRequest setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]]];

        // Reinstantiate the fetchedResultsController
            self.fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];

        NSString *predicateString = self.selectedCategory.categoryName;
        NSLog(@"predicateString is %@", predicateString);

        self.searchPredicate = [NSPredicate predicateWithFormat:@"locationCategory.categoryName == %@", predicateString];
        NSLog(@"self.searchPredicate in refreshTableViewController is %@", predicateString);

        // Do the fetchRequest with the predicate
        [fetchRequest setPredicate:self.searchPredicate];

        // Perform fetch
        NSError *error = nil;
        [self.fetchedResultsController performFetch:&error];

        if (error) {
            NSLog(@"Unable to perform fetch");
            NSLog(@"%@, %@", error, error.localizedDescription);
        }
        [self.tableView reloadData];        


    }
}

为了完整起见,这是我的 PointOfInterest 和 LocationCategory 课程:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class LocationCategory;

@interface PointOfInterest : NSManagedObject

@property (nonatomic, retain) NSString * note;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * address;
@property (nonatomic, retain) NSNumber * latitude;
@property (nonatomic, retain) NSNumber * longitude;
@property (nonatomic, retain) NSString * phoneNumber;
@property (nonatomic, retain) NSString * url;
@property (nonatomic, retain) LocationCategory *locationCategory;

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class PointOfInterest;

@interface LocationCategory : NSManagedObject

@property (nonatomic, retain) id categoryColor;
@property (nonatomic, retain) NSString * categoryName;
@property (nonatomic, retain) NSSet *pointOfInterest;
@end

【问题讨论】:

  • 您是否正在更新您的 tableview 数据源?请提供一些有关如何填充您的表格视图的相关代码 cellForRowAtIndexPath: 委托方法主体。
  • 没有。我有fetchedResultsController,它在标题中声明并在viewDidLoad 中实例化。我要做的就是在选择类别时更新fetchedResultsController。
  • 但是您在哪里将更新后的结果绑定到表格视图?!
  • 我不明白你的问题。
  • 您的refreshTableViewController: 方法实际上什么也没做。问问自己这个问题:“如何简单地创建NSFetchRequest 的实例会导致在获取的结果控制器中发生任何变化,更不用说在表格视图中发生变化了?”

标签: ios objective-c uitableview core-data nspredicate


【解决方案1】:

我想通了:

我将此添加到我在MyTableViewController 上的实现文件中:

@synthesize fetchedResultsController = _fetchedResultsController;

正如我所料,我错过了一些非常愚蠢的东西——而且我是**对的!!

我在 Ray Wenderlich 的网站上阅读了这篇文章,并注意到它在实现中包含该行,但我没有。我从来没有遇到过使用@synthesize 的需要,而且我在其他地方看到过“你几乎从不需要它”等内容。好吧,事实证明,你确实需要!

http://www.raywenderlich.com/14742/core-data-on-ios-5-tutorial-how-to-work-with-relations-and-predicates

StackOverflow 上的这篇文章涵盖了 @synthesize 的基础知识。

What exactly does @synthesize do?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多