自动完成文字字段

时间:2015-10-15 19:57:16

标签: objective-c uitableview autocomplete uitextfield

我一直在尝试使用自动填充创建一个文本字段,而且似乎无法弄清楚我在这一点上做错了什么。当您单击该字段时,键盘会出现,但是当您开始键入时,它不会显示表视图或使用数组填充表。我的代码如下:

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UITextField *productDescription;
@property (strong, nonatomic) IBOutlet UITableView *productDescriptionTableView;
@property (nonatomic, retain) NSArray *productDescriptionArray;
@property (nonatomic, retain) NSArray *productDescriptionFilterArray;

@end

@implementation ViewController
@synthesize productDescriptionArray;
@synthesize productDescription,productDescriptionTableView;
@synthesize productDescriptionFilterArray;

- (void)viewDidLoad
{
    [super viewDidLoad];
productDescriptionArray = [[NSArray alloc]initWithObjects:
                @"Product Description 1",
                @"Product Description 2",
                @"Product Description 3",
                @"Product Description 4",
                @"Product Description 5", nil];
    productDescriptionTableView.hidden = FALSE;
    productDescriptionTableView.tableFooterView = [UIView new];
    [[productDescriptionTableView layer] setMasksToBounds:NO];
    [[productDescriptionTableView layer] setShadowColor:[UIColor blackColor].CGColor];
    [[productDescriptionTableView layer] setShadowOffset:CGSizeMake(0.0f, 5.0f)];
    [[productDescriptionTableView layer] setShadowOpacity:0.3f];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - UITextField Delegate
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"Range:%@",NSStringFromRange(range));
    NSLog(@"%@",productDescription.text);
    NSString *passcode = [productDescription.text stringByReplacingCharactersInRange:range withString:string];
    NSLog(@"%@",passcode);
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",passcode];
    productDescriptionFilterArray = [productDescriptionArray filteredArrayUsingPredicate:predicate];
    NSLog(@"%@", productDescriptionFilterArray);
    if ([productDescriptionFilterArray count]==0)
    {
        productDescriptionTableView.hidden = TRUE;
    }
    else
    {
        productDescriptionTableView.hidden = FALSE;
    }
    [productDescriptionTableView reloadData];
    return TRUE;
}

#pragma mark - UITableView Delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [productDescriptionFilterArray count];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *Cell = [tableView cellForRowAtIndexPath:indexPath];
    productDescription.text =Cell.textLabel.text;
}

@end

0 个答案:

没有答案
相关问题