didSelectRowAtIndexPath not being called?

2019-09-10 01:41发布

For some odd reason my didSelectRowAtIndexPath is not being called in my code. Anyone have a clue why? Everything else works, it may be something to do with my sections. I don't understand why my deletion class works but didSelectRowAtIndexPath doesn't.

- (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条回答
戒情不戒烟
2楼-- · 2019-09-10 01:54

Might be there are multiple reason but check one by one ;)

1) Make sure that you have to set both

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

2) And also check this answer (s)

-didSelectRowAtIndexPath: not being called

3) If you have used any gesture recognizer try to removing it and check if it causing the problem or not ?

4)

self.tableView.allowsMultipleSelection = NO;
self.tableView.allowsSelection = YES;
查看更多
登录 后发表回答