OBJ-C-didSelectRowAtIndexPath没有触发吗?

时间:2019-04-16 15:18:24

标签: ios objective-c uitableview

我很困惑。由于某些原因,当我点击tableView单元格时,didSelectRowAtIndexPath没有执行吗?是的,设置了我的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 :(得分:0)

在viewDidLoad中尝试下一行:

self.tableView.allowsSelection = YES;

或在情节提要中检查该属性。好吧,您也可以像这样尝试该单元格:

// This line do not affect the selection delegate only the style    
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
cell.userInteractionEnabled = YES;