iOS中的自动填充文本字段无法正常工作

时间:2015-02-25 09:47:56

标签: ios objective-c uitableview nsstring

我是iOS开发中的新手,我想在我的ap中添加自动填充文本字段我为此编写代码

- (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行中遇到错误我在google中找到它,但我没有得到解决方案请帮我这个 感谢。

1 个答案:

答案 0 :(得分:0)

如果self.address属性为数组,则NSNull属性可能为NSNull或包含NSNull。您可以通过以下方式检查对象是否为if(![object isEqual:[NSNull null]]) { //do something if object is not equals to [NSNull null] }

NSMutableArray

代码来自this answer.

编辑:如果您使用[self.address removeObjectIdenticalTo:[NSNull null]],则可以在NSNull

之后调用方法self.address=[allObjects valueForKey:@"Address"];从阵列中删除所有{{1}}个对象