didSelectRowAtIndexPath第一次没有被调用

时间:2014-12-24 17:52:42

标签: ios objective-c uitableview

我有一个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];
}

1 个答案:

答案 0 :(得分:0)

我会将didSelectRowAtIndexPath:中的代码放到prepareForSegue中:而是尝试使用sender参数来标识所选标记