【问题标题】:Autocomplete text field in iOS not workingiOS中的自动完成文本字段不起作用
【发布时间】:2015-04-27 06:08:06
【问题描述】:

我是 iOS 开发的新手,我想在我的应用程序中添加自动完成文本字段,我为此编写了一个类似 as 的代码

- (void)viewDidLoad
{
 [self get data];
}
-(void)getdata
{
NSMutableArray *allObjects = [NSMutableArray array];
NSUInteger limit = 1000;
__block NSUInteger skip = 0;
PFQuery *query = [PFQuery queryWithClassName:@"MapInfo"];
[query setLimit: limit];
[query setSkip: skip];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)      {
    if (!error) {
        [allObjects addObjectsFromArray:objects];
        if (objects.count == limit) {

            skip += limit;
            [query setSkip: skip];
            [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                [allObjects addObjectsFromArray:objects];
                self.qpinname=[allObjects valueForKey:@"GPIN"];
                self.locationarray=[allObjects valueForKey:@"Location"];
                self.latitude=[self.locationarray valueForKey:@"lat"];
                self.longitude=[self.locationarray valueForKey:@"lng"];
                self.address=[allObjects valueForKey:@"Address"];
                NSLog(@"Address %@",self.address);
                self.usernameArray=[allObjects valueForKey:@"AddedBy"];
            }];
        }
    }
    else
    {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];
}

然后我得到了我的数据数组,我想在我的表格视图中显示它,就像

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring
{
[self.autoaddress removeAllObjects];
for(NSString *curString in self.address)
{
    NSRange substringRange = [curString rangeOfString:substring];
    if (substringRange.location == 0) {
        [self.autoaddress addObject:curString];
    }
}
[_autocompleteTableView reloadData];
}
 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
_autocompleteTableView.hidden = NO;

NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section
{
return self.autoaddress.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = nil;
static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc]
             initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] ;
}

cell.textLabel.text = [self.autoaddress objectAtIndex:indexPath.row];
return cell;
}

然后我的代码出错了

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring
{
[self.autoaddress removeAllObjects];
for(NSString *curString in self.address)
{
    NSRange substringRange = [curString rangeOfString:substring];
    if (substringRange.location == 0) {
        [self.autoaddress addObject:curString];
    }
}
[_autocompleteTableView reloadData];
}

这里我在NSRange substringRange = [curString rangeOfString:substring];-[NSNull rangeOfString:]: unrecognized selector sent to instance 行中遇到错误,我在谷歌中找到了它,但我没有得到解决方案,请帮助我 谢谢。

【问题讨论】:

    标签: ios objective-c uitableview nsstring


    【解决方案1】:

    您的self.address 属性可能是NSNull,或者如果它是一个数组,它包含NSNull。您可以通过以下方式检查对象是否为NSNull

    if(![object isEqual:[NSNull null]])
    {
        //do something if object is not equals to [NSNull null]
    }
    

    代码来自this answer.

    编辑:如果你使用NSMutableArray,你可以调用方法[self.address removeObjectIdenticalTo:[NSNull null]]self.address=[allObjects valueForKey:@"Address"];之后的数组中删除所有NSNull对象

    【讨论】:

    • 但是当我记录它时它会给我数组值。
    • 何时记录数组或数组内的对象?
    • - (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring 方法中我打印了我的数组值但这里我的数组有时包含零值然后它是问题??????
    • 是的,这可能会导致调用rangeOfString 时出现unrecognized selector sent to instance
    • 那有什么解决办法请帮帮我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多