我想使用开关过滤 TableView 中的数据。我正在使用解析。假设我有一个 TableViewController 和一个 FilterViewController(普通 ViewController 类)。我想通过打开一些开关来过滤数据。并在按下“完成”按钮后显示过滤后的 TableView。我已经有了一些想法,但我不知道如何在 TableViewController 中设置更改。
谢谢
Filter.h
@protocol ViewControllerDelegate;
@interface FiltrViewController : UIViewController
{
IBOutlet UISwitch *switch1;
IBOutlet UISwitch *switch2;
IBOutlet UISwitch *switch3;
IBOutlet UISwitch *switch4;
IBOutlet UISwitch *switch5;
IBOutlet UISwitch *switch6;
IBOutlet UIBarButtonItem *button1;
IBOutlet UILabel *label;
}
@property (nonatomic, weak) id<ViewControllerDelegate> delegate;
@property (nonatomic, strong) PFObject *obj;
@property(nonatomic,strong) NSArray *keys;
+ (void) filter:(id<ViewControllerDelegate>)delegate;
-(IBAction)buttontouched:(id)sender;
-(IBAction)switch1:(id)sender;
@end
@protocol ViewControllerDelegate < NSObject>
-(void)filter;
@end
过滤器.m
@interface FiltrViewController (){
NSMutableDictionary* configG;
}
@property (retain) NSMutableDictionary* configG;
@end
@implementation FiltrViewController
@synthesize delegate;
@synthesize configG;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
configG = [[NSMutableDictionary alloc] init];
}
return self;
}
-(void)filter
{
PFQuery *query = [PFQuery queryWithClassName:@"Countries"];
NSArray *keys = @[ @"Africa", @"Europe", @"South America", @"North America", @"Asia", @"Australia" ];
NSArray *defaultValues = @[ @YES, @YES, @YES, @YES, @YES, @YES ];
NSMutableDictionary *config = [NSMutableDictionary dictionaryWithObjects:defaultValues forKeys:keys];
NSSet *filter = [config keysOfEntriesPassingTest:
^BOOL (id key, NSNumber *value, BOOL *stop) {
return [value boolValue];
}];
[query whereKey:@"DescriptTitle" containedIn:[filter allObjects]];
self.keys = keys;
self.configG = config;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(IBAction)buttontouched:(id)sender;
{
[self dismissViewControllerAnimated:YES completion:nil];
[delegate filter];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)switch1:(UISwitch *)sender
{
[self.configG setObject:@([sender isOn]) forKey:[self.keys objectAtIndex:sender.tag]];
}
@end
TableView.m
- (void)FilterTable:(FiltrViewController *)viewController didChooseValue:(CGFloat)value {
[FiltrViewController filter:self];
}
过滤方法
- (void)filterController:(FilterViewController *)controller didEditConfig:(NSMutableDictionary *)config
{
NSSet *filter = [config keysOfEntriesPassingTest:
^BOOL (id key, NSNumber *value, BOOL *stop) {
return [value boolValue];
}];
PFQuery *query = [PFQuery queryWithClassName:@"Countries"];
[query whereKey:@"DescriptTitle" containedIn:[filter allObjects]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
//How can I pass filtered objects from Filter.m back into my TableView?
[self.MainTable reloadData];
NSLog(@"%u", objects.count);
}else{
}
}
];
最佳答案
通常,您不希望每次更改任何开关时都在后台运行多个查询。相反,您应该保留开关状态的配置(我想您已经在某处拥有它,因此您可以设置开关的初始状态)。
现在,当每个开关发生变化时,更新配置。按下完成按钮时,将配置发送回另一个 View Controller (或保存到共享存储/位置)。
在另一个 View Controller 中,检查是否有变化。如果有,请重新查询。但是……
仅进行 1 次查询。按原样设置查询类,但添加国家名称作为该查询的一部分:
// create array of the country names to include
NSArray *countries = ...;
[query whereKey:@"DescriptTitle" containedIn:countries];
现在事情变得更加高效且易于处理(一个请求和一个响应)。
配置:
您有一组国家,您希望能够启用/禁用它们。一个简单的配置是有一个字典,其中键是国家名称,值是包含相关联的 BOOL
状态值的 NSNumber
实例。
当您设置开关时,您可以使用这些值来设置状态。然后您可以在切换开关时更改状态。
完成后,我将使用委托(delegate)关系将修改后的配置传递回源 View Controller (您的 TableView Controller )。 (注意:通常代理也会关闭过滤器 View Controller ...)。
要获取查询的国家/地区列表,请查看使用 keysOfEntriesPassingTest:
。
设置:
NSArray *keys = @[ @"Africa", @"Europe", @"South America", @"North America", @"Asia", @"Australia" ];
NSArray *defaultValues = @[ @YES, @YES, @YES, @YES, @YES, @YES ];
NSMutableDictionary *config = [NSMutableDictionary dictionaryWithObjects:defaultValues forKeys:keys];
self.keys = keys;
self.config = config;
搜索(filter
方法):
NSSet *filter = [Dictionary keysOfEntriesPassingTest:
^BOOL (id key, NSNumber *value, BOOL *stop) {
return [value boolValue];
}];
PFQuery *query = [PFQuery queryWithClassName:@"Countries"];
[query whereKey:@"DescriptTitle" containedIn:[filter allObjects]];
// execute the query...
开关变化:
-- 假设开关有一个标签,它是 keys
数组的索引
- (IBAction)switchChanged:(UISwitch *)sender
{
[self.config setObject:@([sender isOn]) forKey:[self.keys objectAtIndex:sender.tag]];
}
按下完成按钮:
[self filter];
关于ios - 使用来自其他 View 的查询过滤 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22286694/