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

BOOL isChecked;

//check if there is checkmark there

if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark){

    isChecked = YES;

}

else {
    isChecked = NO;
}

//adds or moves checkmark

if(isChecked == YES){

    NSLog(@"Checkmark removed...");



    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
    newCell.accessoryType = UITableViewCellAccessoryNone;

    lastIndexPath = indexPath;

}


if(isChecked == NO){

    NSLog(@"Checkmark added...");

    [chosenSports addObject:[sports objectAtIndex:[indexPath row]]];

    NSLog(@"%@",[chosenSports objectAtIndex:0]);

    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
    newCell.accessoryType = UITableViewCellAccessoryCheckmark;

    lastIndexPath = indexPath;

}


[tableView deselectRowAtIndexPath:indexPath animated:YES];


}

由于某种原因,我的NSLog的结果为NULL,表明“ chosenSports”数组为空,但我不知道为什么!有什么建议么?

最佳答案

确保像这样初始化数组(在viewDidLoad函数中):

self.chosenSports = [[NSMutableArray alloc] initWith...];

07-26 09:38