didSelectRowAtIndexPath 没有被调用?

Posted

技术标签:

【中文标题】didSelectRowAtIndexPath 没有被调用?【英文标题】:didSelectRowAtIndexPath not being called? 【发布时间】:2014-05-02 14:35:11 【问题描述】:

由于某些奇怪的原因,我的代码中没有调用我的 didSelectRowAtIndexPath。有人知道为什么吗?其他一切正常,可能与我的部分有关。我不明白为什么我的删除类有效,但 didSelectRowAtIndexPath 无效。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 

    return self.subjectArray.count;





- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 

    return [self.subjectArray objectAtIndex:section];


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    return [[self.sectionArrays objectAtIndex:section] count];


- (UITableViewCell *)tableView:(UITableView *)tableViewer cellForRowAtIndexPath:(NSIndexPath *)indexPath
    NSArray *currentSectionArray = [self.sectionArrays objectAtIndex:indexPath.section];

    //populates the table based on which view is selected.

    UITableViewCell *cell = [tableViewer dequeueReusableCellWithIdentifier:@"todayCell"];
    NSString *toDoItem = [currentSectionArray objectAtIndex:indexPath.row];
    cell.textLabel.text = toDoItem;

    cell.textLabel.adjustsFontSizeToFitWidth = YES;
    cell.textLabel.minimumScaleFactor = 0.5;



        cell.accessoryType = UITableViewCellAccessoryCheckmark;

        cell.accessoryType = UITableViewCellAccessoryNone;


    return cell;




- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
    // Return YES if you want the specified item to be editable.
    return YES;


// Override to support editing the table view.
- (void)tableView:(UITableView *)tableViews commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 


    if (editingStyle == UITableViewCellEditingStyleDelete) 

        NSMutableArray *currentSectionArray = [self.sectionArrays objectAtIndex:indexPath.section];
        NSString *currentStringSubject = [self.subjectArray objectAtIndex:indexPath.section];





        NSMutableDictionary *tempDic = [self.mainDictionary objectForKey:currentStringSubject];


        NSArray *temp = [tempDic allKeysForObject:[currentSectionArray objectAtIndex:indexPath.row]];
        NSString *key = [temp lastObject];




        int keyInt = [key integerValue];
        int maxKey = [[[tempDic allKeys]lastObject]integerValue];
        NSLog(@"KEYINT%d",keyInt);

        [tempDic removeObjectForKey:key];


        if (keyInt != maxKey) 
            for (int x = keyInt; x != -99; x++) 

                int xIncrease = x + 1;
                NSLog(@"%@",[NSString stringWithFormat:@"xIncrease%d",xIncrease]);
                NSString *tempWHAT = [tempDic objectForKey:[NSString stringWithFormat:@"%d",xIncrease]];
                NSLog(@"tempDic%@",tempDic);
                NSLog(@"tempWhat%@",tempWHAT);
                if ([tempWHAT length] == 0) 
                    NSLog(@"LENGTH WAS 0");

                    x = -100;
                
                else

                    NSLog(@"LENGTH WASN'T ZERO, TRYING TO REARRANGE");
                [tempDic setObject: [tempDic objectForKey:[NSString stringWithFormat:@"%d",xIncrease]] forKey:[NSString stringWithFormat:@"%d",x]];
                [tempDic removeObjectForKey:[NSString stringWithFormat:@"%d",xIncrease]];
                
            
        



        //[dict setObject: [dict objectForKey: @"oldkey"] forKey: @"newkey"];
        //[dict removeObjectForKey: @"oldkey"];


        [currentSectionArray removeObjectAtIndex:indexPath.row];
        [[self.sectionArrays objectAtIndex:indexPath.section]isEqualToArray:currentSectionArray];


        [self.mainDictionary setObject:tempDic forKey:[NSString stringWithFormat:@"%@",currentStringSubject]];


        [[NSUserDefaults standardUserDefaults] setObject:self.mainDictionary forKey:[NSString stringWithFormat:@"mainDictionary%@",self.todayString ]];
        [[NSUserDefaults standardUserDefaults]synchronize];

        [tableViews deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];



    


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

        NSLog(@"TEST");


    NSString *currentStringSubject = [self.subjectArray objectAtIndex:indexPath.section];

    NSMutableDictionary *mutDic = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"mainDictionary%@",self.todayString ]];

    NSMutableDictionary *tempDir = [mutDic valueForKey:[NSString stringWithFormat:@"%@",[self.subjectArray objectAtIndex:indexPath.section]]];




    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if ([self.mainDictionary objectForKey:[NSString stringWithFormat:@"%@completed",currentStringSubject]]) 


    
    [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationNone];


【问题讨论】:

【参考方案1】:

可能有多种原因,但要一一检查;)

1) 确保你必须同时设置

self.tableView.delegate = self;
self.tableView.dataSource = self;

2) 并检查这个答案(s)

-didSelectRowAtIndexPath: not being called

3) 如果您使用过任何手势识别器,请尝试将其移除并检查是否导致问题?

4)

self.tableView.allowsMultipleSelection = NO;
self.tableView.allowsSelection = YES;

【讨论】:

这是手势识别器问题。谢谢!

以上是关于didSelectRowAtIndexPath 没有被调用?的主要内容,如果未能解决你的问题,请参考以下文章

在 didSelectRowAtIndexPath 之后 prepareForSegue?

我的 didSelectRowAtIndexPath 没有被调用

didSelectRowAtIndexPath 没有被调用?

tableView - didSelectRowAtIndexPath 未调用

快速:didSelectRowAtIndexPath

我可以麻烦UITableViewDelegate的didSelectRowAtIndexPath:吗?