【问题标题】:Obj-C - didSelectRowAtIndexPath not firing?Obj-C - didSelectRowAtIndexPath 没有触发?
【发布时间】:2019-04-16 15:18:23
【问题描述】:

我被难住了。出于某种原因,当我点击我的 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];

        }

【问题讨论】:

  • 您是否尝试将代码包装到DispatchQueue.main.async 以确保它在主线程上执行?
  • 我知道你提到了它,但请仔细检查你是否在 tableview 上同时设置了 dataSourcedelegate。您所描述的问题正是如果设置了dataSource 但未设置delegate 会发生什么。
  • 是的 - 不起作用:/ @LukasWürzburger
  • 这里还不够。我们所能做的就是猜测。是否启用了表交互?表格视图的allowsSelection 是真的吗?
  • Storyboard 中的表格视图选择选项是否设置为No selection

标签: ios objective-c uitableview


【解决方案1】:

尝试使用 viewDidLoad 中的下一行:

self.tableView.allowsSelection = YES;

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

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

【讨论】:

  • 试过了...没有骰子:(
  • 让我在一个新项目中尝试你的代码,我会通知我发现的:)
  • 同样的代码对我来说效果很好,也许你的故事板你有错误的属性,请显示 IB 对象的属性
  • 哇真奇怪...好吧,将仔细检查属性。待命:)
  • 太疯狂了——我从头开始在一个全新的 ViewController 中重新构建了它,但我仍然无法让 didSelectRow 执行。您的代表是否都设置为 self(视图控制器)?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 2015-06-05
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
相关资源
最近更新 更多