隐藏以编程方式创建的自动完成UITableView

时间:2014-01-14 19:04:21

标签: objective-c uitableview uiviewcontroller ios7 uiapplicationdelegate

我正在使用此tutorial中的自动完成UITableView构建应用程序。我有自动完成功能正常工作,但我希望UITableView-autocomplete下拉菜单在单击单词或外部触摸时消失。在以编程方式设置对象时,我不确定如何设置委托。我只使用界面构建器完成了这个。

·H

@interface slrpViewController : UIViewController<UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource>
{
NSMutableArray *dataArray;
NSMutableData *receivedData;

NSMutableArray *pastUrls;
NSMutableArray *autocompleteUrls;
UITableView *autocompleteTableView;
}
@property(nonatomic, retain) IBOutlet UITextField *eWordEntered;
@property (nonatomic, retain) NSMutableArray *pastUrls;
@property (nonatomic, retain) NSMutableArray *autocompleteUrls;
@property (retain, nonatomic) NSMutableData *responseData;
@property (nonatomic, retain) UITableView *autocompleteTableView;
-(void)setReceivedData:(NSMutableData*)pReceivedData;
-(NSMutableData *) getReceivedData;
-(void) getAutoCompleteArray;
-(void)searchAutocompleteEntriesWithSubstring:(NSString *)substring;

的.m

- (void)viewDidLoad
{
[super viewDidLoad];
[self getAutoCompleteArray];
pastUrls = [[NSMutableArray alloc] init];

NSLog(@"In the viewDidLoad and pasturl is: %@", self.pastUrls);
self.autocompleteUrls = [[NSMutableArray alloc] init];

autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(210, 225, 310, 120) style:UITableViewStylePlain];
self.autocompleteTableView.delegate = self;
self.autocompleteTableView.dataSource = self;
autocompleteTableView.scrollEnabled = YES;
autocompleteTableView.hidden = YES;
[self.view addSubview:autocompleteTableView];

-(void)setReceivedData:(NSMutableData*)pReceivedData
{
receivedData = pReceivedData;
}

-(NSMutableData *) getReceivedData{
return receivedData;
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];   
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{[receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *e = nil;
NSError *error = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: receivedData options:  NSJSONReadingMutableContainers error: &e];

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:receivedData
                                                         options:kNilOptions
                                                           error:&error];
seneca_word.ids = [jsonDict objectForKey:@"ids"];
NSArray *array_ids = [jsonDict objectForKey:@"ids"];
NSString *ids = array_ids[0];
seneca_word.ids = ids;
for (id key in jsonDict)
{
    NSLog(@"key: %@, value: %@", key, [jsonDict objectForKey:key]);
    NSLog(@"The value of bases by itself is: %@", [jsonDict objectForKey:@"bases"]);
}

if (!jsonArray)
{
    NSLog(@"Error parsing JSON: %@", e);
}
else
{
    if([jsonDict objectForKey:@"english"] != nil){
        pastUrls = [jsonDict objectForKey:@"bases"];
    }

    else{
        //Some of JSON object that I don't want to use here

}//else   
}//(void)connectionDidFinishLoading

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {
[autocompleteUrls removeAllObjects];
for(NSString *curString in pastUrls) {
    NSRange substringRange = [curString rangeOfString:substring];
    if (substringRange.location == 0) {
        [autocompleteUrls 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 autocompleteUrls.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 = [autocompleteUrls objectAtIndex:indexPath.row];
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
self.eWordEntered.text = selectedCell.textLabel.text;

if(tableView == autocompleteTableView){
    //The autocomplete table view is the one that fired the didSelect delegate method
    //So hide the autocomplete table.
    //do whatever else you need to do to empty the autocompleteTableView's data source
    //or/and simply hide the table after that
    [autocompleteTableView setHidden:YES];    

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//When the user clicks outside of the uitableview it will disappear
[autocompleteTableView setHidden:YES];
}

正如您所看到的,我使用我从RESTful API获取的JSON数据填充自动完成UITableView。

我收到警告,Assigning to 'id<UITableViewDelegate>' from incompatible type 'ViewController *const __strong'为行:

self.autocompleteTableView.delegate = self;
self.autocompleteTableView.dataSource = self;

我想,一旦我得到委托的东西整理出来,我就能做我想做的事。我做了一些研究,并试图创建一个委托类,但无法使该解决方案工作。我甚至不确定这是否是正确的方法,因为我通常通过界面构建​​器而不是以编程方式执行此操作。非常感谢任何方向或帮助。谢谢!

2 个答案:

答案 0 :(得分:0)

尝试设置self.autocomplete TableView.hidden = YES;

答案 1 :(得分:0)

您应该使用tableView的didSelectCellAtIndexPathRow委托方法从tableView中识别单元格上的用户点击。如果您以编程方式创建了tableView,那就没问题了。

  1. 只需确保UIViewController符合UITableViewDelegate和UITableViewDataSource`协议。

  2. 确保将tableView的委托和dataSource属性设置为self。

  3. 在viewController的didSelectCellAtIndexPathRow文件中实现.m委托方法,如下所示:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    }
    
  4. 然后在该委托方法中,您需要帮助检测您的didSelect方法从哪个tableView中被触发,因为您只想在用户从该表中选择单元格时隐藏自动完成表。所以你像这样做一个简单的tableView检查:

    if(tableView == autocompleteTableView){
        //The autocomplete table view is the one that fired the didSelect delegate method
        //So hide the autocomplete table.
        //do whatever else you need to do to empty the autocompleteTableView's data source
        //or/and simply hide the table after that 
        [autocompleteTableView setHidden:YES];
    }
    
  5. 您可能还希望确保在用户输入文本字段中的某些内容时将autocompleteTableView隐藏属性设置为NO,以便再次显示自动完成。< / p>

  6. 这就是所有伙伴。