gpt4 book ai didi

ios - Obj-C - didSelectRowAtIndexPath 未触发?

转载 作者:行者123 更新时间:2023-11-29 05:50:33 24 4
gpt4 key购买 nike

我被难住了。由于某种原因,当我点击 tableView 单元格时,didSelectRowAtIndexPath 没有执行?是的,我的 tableView 委托(delegate)已设置,并且数据已填充在单元格标签中。我是否遗漏了下面明显的东西?本质上,当我的用户点击 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];

}

最佳答案

尝试使用 viewDidLoad 中的下一行:

self.tableView.allowsSelection = YES;

或者检查 Storyboard中的该属性。那么你也可以尝试使用单元格,如下所示:

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

关于ios - Obj-C - didSelectRowAtIndexPath 未触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55711542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com