【发布时间】: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