【问题标题】:didSelectRowAtIndexPath not called the first time第一次没有调用 didSelectRowAtIndexPath
【发布时间】:2015-02-22 18:59:04
【问题描述】:

我有一个UITableView,当您选择一行时,它会引导您进入所选行的“详细信息”。但是,当我第一次单击该行时,它会进入prepareForSegue 方法,而不是调用didSelectRowAtIndexPath。当我在详细视图上按下后退按钮时,应用程序会返回,如果我再次选择该行,则会调用该方法。

我不使用didDeselectRowAtIndexPath。

这种模式会继续,我不知道为什么。当我加载我的数据时,我会打电话给[tableview reloadData]。

我也尝试将self.TagsTableView.allowsMultipleSelection = YES; 添加到我的viewDidLoad 方法中。

这是我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bluetoothManager = [[BlueToothLEManager alloc] init];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTableView:) name:@"ScanComplete" object:nil];


    self.Tags = self.bluetoothManager.Tags; 
    NSLog(@"Fetch Tags: %@", self.Tags); 
}
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell* cell = [fetchTagsTableView dequeueReusableCellWithIdentifier:@"tagCell"];

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"tagCell"];
    }

    FetchTag* newTag = [fetchTags objectAtIndex:indexPath.row];

    cell.textLabel.text = [NSString stringWithFormat:@"%@ %d", newTag.tagName, indexPath.row];
    cell.detailTextLabel.text = [newTag.tagUUID UUIDString];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectedTagFromTable =  [fetchTags objectAtIndex:indexPath.row];
    [TagsTableView deselectRowAtIndexPath:indexPath animated:YES];
}
...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    if([[segue identifier] isEqualToString:@"DetailView"]){
        TagDetailViewController* ftdvc = [segue destinationViewController];
        [ftdvc setSelectedTag:selectedTagFromTable];


    }
    // Pass the selected object to the new view controller.
}

-(void)reloadTableView:(NSNotification*)notif{
    NSLog(@"Reloading Table View");
    NSLog(@"Fetch Tags: %@", self.Tags); 
    [self.TagsTableView reloadData];
}

【问题讨论】:

  • 所以您希望使用情节提要转场自动导航选择和调用didSelectRowAtIndexPath?我不确定我会相信这一点。我建议删除自动转场,而是手动调用转场(或只是手动进行导航)in didSelectRowAtIndexPath.
  • 你还在使用自定义单元格吗?您是否在代码的其他位置预先填充了“fetchTagsTableView”单元格(即 UITableViewCell* cell = [fetchTagsTableView dequeueReusableCellWithIdentifier:@"tagCell"];)?
  • @matt 我使用didSelectRowAtIndex 用数据填充对象,使用prepareSegue 将数据发送到新的视图控制器。这不是正确的方法吗?抱歉,我是 iOS 新手。我来自 Java/Android 背景。我仍在努力解决这些问题。
  • @BlackHatSamurai 我已经告诉过你我认为“正确的方法”;除了重复我自己之外,我看不出你在问我什么。

标签: ios objective-c uitableview


【解决方案1】:

我会将 didSelectRowAtIndexPath: 中的代码放到 prepareForSegue: 中尝试使用 sender 参数来识别选定的标签

【讨论】:

    猜你喜欢
    • 2011-01-07
    • 1970-01-01
    • 2015-01-20
    • 2012-02-24
    • 1970-01-01
    • 2015-03-10
    • 2013-10-07
    相关资源
    最近更新 更多