gpt4 book ai didi

iOS 7 - 使用带有普通 NSArray 的 MLPAutoCompleteTextField

转载 作者:行者123 更新时间:2023-12-01 16:43:51 25 4
gpt4 key购买 nike

我对 iOS 开发比较陌生,我想在我的应用程序中实现一个自动完成文本字段。在做研究时,我遇到了this library , MLPAutoCompleteTextField。我下载了它,运行了 Demo,并试图了解它是如何工作的。

据我所知,该演示使用 Array 的自定义类和自定义单元格 View ,这就是演示中的自动完成包含该国国旗的原因。

然而,我想要实现的是一个更简单的版本,它只使用一个数组,没有更多的数据和单元格布局的自定义类。

这是我到目前为止所拥有的:

我的 FirstViewController.h 文件

#import <UIKit/UIKit.h>   
#import "MLPAutoCompleteTextFieldDataSource.h"
#import "MLPAutoCompleteTextFieldDelegate.h"

@interface FirstViewController : UIViewController <UITextFieldDelegate, MLPAutoCompleteTextFieldDataSource, MLPAutoCompleteTextFieldDelegate>

@property (strong, nonatomic) NSArray *groupID;
@property (strong, nonatomic) NSMutableArray *part;
@property (strong, nonatomic) NSMutableArray *brand;
@property (strong, nonatomic) NSMutableArray *barcode;
@property (strong, nonatomic) NSMutableArray *itemName;

@property (weak) IBOutlet MLPAutoCompleteTextField *groupIDInput;
@property (weak) IBOutlet MLPAutoCompleteTextField *partInput;
@property (weak) IBOutlet MLPAutoCompleteTextField *brandInput;
@property (weak) IBOutlet MLPAutoCompleteTextField *barcodeInput;
@property (weak) IBOutlet MLPAutoCompleteTextField *itemNameInput;

@property (strong, nonatomic, retain) IBOutlet UIButton *searchButton;

@property (assign) BOOL testWithAutoCompleteObjectsInsteadOfStrings;

@end

如您所见,我有 5 个 AutoCompleteTextViews,我打算使用 5 个数组来为 autoCompleteTextViews 提供数据。

这是我的 FirstViewController.m 文件:
#import "FirstViewController.h"
#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"
#import "MLPAutoCompleteTextFieldDataSource.h"
#import "MLPAutoCompleteTextFieldDelegate.h"
#import "MLPAutoCompleteTextField.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

@synthesize groupID;
@synthesize part;
@synthesize brand;
@synthesize barcode;
@synthesize itemName;

@synthesize groupIDInput;
@synthesize partInput;
@synthesize brandInput;
@synthesize barcodeInput;
@synthesize itemNameInput;

@synthesize searchButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setType];
}

- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


#pragma mark - MLPAutoCompleteTextField DataSource

- (void)groupIDInput:(MLPAutoCompleteTextField *)textField
possibleCompletionsForString:(NSString *)string
completionHandler:(void (^)(NSArray *))handler{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(queue, ^{

NSArray *completions;
if(self.testWithAutoCompleteObjectsInsteadOfStrings){
completions = [self allCountryObjects];
} else {
completions = [self allCountries];
}

handler(completions);
});
}


-(void) setType{
[self.groupIDInput setAutoCompleteTableAppearsAsKeyboardAccessory:NO];
}


- (NSArray *)allCountryObjects{
if(!self.groupID){
NSArray *countryNames = [self allCountries];
NSMutableArray *mutableCountries = [NSMutableArray new];
for(NSString *countryName in countryNames){
[mutableCountries addObject:countryName];
}

[self setGroupID:[NSArray arrayWithArray:mutableCountries]];
}

return self.groupID;
}

- (NSArray *)allCountries{
NSArray *countries =
@[/* Insert Long List of Countries Here */];

return countries;
}

@end

但是,我现在的问题是,在演示中,有一行 [self.autocompleteTextField registerAutoCompleteCellClass:[DEMOCustomAutoCompleteCell class]其中使用了自定义单元格类。我觉得我也应该创建自己的自定义单元类,即使我没有实现任何花哨的东西。

所以,问题是:

我必须实现自己的 CustomAutoCompleteObject 和 CustomAutoCompleteCell 吗?如果没有,我怎样才能通过使用简单的数组来实现这个库?

任何帮助表示赞赏。在过去的 4-5 个小时里,我一直在研究这个问题,我缺乏 iOS 开发知识,这对我造成了影响。

更新 1:

我尝试使用预先声明的数组而不是由数据库查询填充的可变数组,我还进行了一些更改,如下所示:
- (void)autoCompleteTextField:(MLPAutoCompleteTextField *)textField
possibleCompletionsForString:(NSString *)string
completionHandler:(void (^)(NSArray *))handler{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(queue, ^{

NSArray *completions;
//completions = [self allCountries];
completions = [self initializeGroupIDArray];

handler(completions);
});
}

此功能附加到 Storyboard 。

而我的 initializeGroupIDArray 如下:
-(NSArray *)initializeGroupIDArray{
// Getting the database path.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *dbPath = [docsPath stringByAppendingPathComponent:@"itemList.db"];

NSMutableArray *groupArray = @[/* Insert List of Countries Here */];


FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];

NSString *sqlSelectQuery = @"SELECT DISTINCT GROUPID FROM ItemList";

// Query result
FMResultSet *resultsWithNameLocation = [database executeQuery:sqlSelectQuery];

while([resultsWithNameLocation next]) {
NSString *queryResult = [NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"GROUPID"]];

// loading your data into the array, dictionaries.
NSLog(@"GroupID = %@", queryResult);
[groupArray addObject:queryResult];
}
[database close];

NSArray *groupID;
[groupID = groupArray copy];
return groupID;

}

但是,在我看来,我没有正确添加数据库查询的结果。有人有想法吗?

最佳答案

我没有初始化我的可变数组。现在是这样的:

#pragma mark - MLPAutoCompleteTextField DataSource

- (void)autoCompleteTextField:(MLPAutoCompleteTextField *)textField
possibleCompletionsForString:(NSString *)string
completionHandler:(void (^)(NSArray *))handler{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(queue, ^{

NSLog(@"autoCompleteTextField Entered");

NSArray *completions;
completions = [self allGroups];

handler(completions);
});
}

allGroups 函数:
-(NSArray *)allGroups{

NSLog(@"allGroups Entered");

// Getting the database path.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *dbPath = [docsPath stringByAppendingPathComponent:@"itemList.db"];

NSMutableArray *groupArray = [[NSMutableArray alloc] init]; //This bad body right here.

FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];

NSString *sqlSelectQuery = @"SELECT DISTINCT GROUPID FROM ItemList";

// Query result
FMResultSet *resultsWithNameLocation = [database executeQuery:sqlSelectQuery];

while([resultsWithNameLocation next]) {
NSString *groupIDName = [NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"GROUPID"]];

// loading your data into the array, dictionaries.
NSLog(@"Group ID = %@", groupIDName);
[groupArray addObject:groupIDName];
}
[database close];


NSLog(@"size of groupArray (mutbale): %d", [groupArray count]);
for (NSUInteger i = 0; i < [groupArray count]; i++){
NSLog(@"Group Array (mutable) :%@", groupArray[i]);
}

NSArray *groupID;
[groupID = groupArray copy];

NSLog(@"size of groupID (immutbale): %d", [groupID count]);
for (NSUInteger i = 0; i < [groupID count]; i++){
NSLog(@"Group ID (non mutable) :%@", groupID[i]);
}

NSLog(@"allGroups before return statement");
return groupID;

}

关于iOS 7 - 使用带有普通 NSArray 的 MLPAutoCompleteTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21886250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com