Obj-C - не запускается ли SelectRowAtIndexPath? - PullRequest
0 голосов
/ 16 апреля 2019

Я в тупике. По какой-то причине, когда я касаюсь своей ячейки tableView, не выполнялся лиSelectRowAtIndexPath? И да, мой делегат tableView установлен, и данные заполняются в метке ячейки. Я что-то упускаю из своего нижнего я? По сути, когда мой пользователь касается ячейки tableView, содержимое метки ячейки должно появиться в текстовом поле.

.h

@interface RegisterViewController : UIViewController <UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource, UIImagePickerControllerDelegate> {

}

@property (nonatomic) IBOutlet UITableView *tableView;
@end

.m

   - (void)viewDidLoad {
        [super viewDidLoad];

        self.tableView.hidden = NO;

       self.tableView.delegate = self;
        self.tableView.dataSource = self;

    }

- (void)LoadJson_search{

    searchArray=[[NSMutableArray alloc]init];
    //    NSLog(@"str......%@",strSearch);
    // This API key is from https://developers.google.com/maps/web/
    NSString *str1 = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/queryautocomplete/json?input=%@&key=AIzaSyAm7buitimhMgE1dKV2j4_7doULluiiDzU", strSearch];
    NSURL *url = [NSURL URLWithString:str1];

    NSData *data = [NSData dataWithContentsOfURL:url];
    NSError *error=nil;
    if(data.length==0)
    {

    }
    else
    {
        NSDictionary *jsondic= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

        //         NSLog(@"1,,,,%@",jsondic);
        [searchArray removeAllObjects];
        if([[jsondic objectForKey:@"status"]isEqualToString:@"ZERO_RESULTS"])
        {

        }
        else if([[jsondic objectForKey:@"status"]isEqualToString:@"INVALID_REQUEST"])
        {
        }
        else
        {
             for(int i=0;i<[jsondic.allKeys count];i++)
            {
                NSString *str1=[[[jsondic objectForKey:@"predictions"] objectAtIndex:i] objectForKey:@"description"];
                [searchArray addObject:str1];
            }
            self.tableView.hidden = NO;


            //            NSLog(@"%@", searchArray);
        }
        if (searchArray.count == 0) {
            self.tableView.hidden = YES;
        }else{
            [self.tableView reloadData];
        }
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
   // if (self.addressField.tag == 3) {

    if (textField == self.addressField) {

        strSearch = [self.addressField.text stringByReplacingCharactersInRange:range withString:string];
        if([string isEqualToString:@" "]){

        }else{
            [self LoadJson_search];
        }}
        // }
    return YES;
}


- (BOOL)textFieldShouldClear:(UITextField *)textField{
    self.tableView.hidden = YES;
    [self.tableView reloadData];

    return YES;
}



        -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
            return 1;
        }


        -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
            return searchArray.count;
        }


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

            UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
            if(!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            }

            cell.textLabel.text = [searchArray objectAtIndex:indexPath.row];

            return cell;
        }


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

            NSLog(@"TAPPED CELL");

            self.addressField.text = [searchArray objectAtIndex:indexPath.row];

            self.tableView.hidden = YES;
            [self.tableView reloadData];

        }

1 Ответ

0 голосов
/ 16 апреля 2019

Попробуйте следующую строку в вашем viewDidLoad:

self.tableView.allowsSelection = YES;

Или проверьте это свойство в вашей раскадровке. Ну, вы можете попробовать с клеткой тоже так:

// This line do not affect the selection delegate only the style    
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
cell.userInteractionEnabled = YES;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...