0
-(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”数组为空,但我不知道为什么!有什么建议么?

4

2 回答 2

2

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

self.chosenSports = [[NSMutableArray alloc] initWith...];
于 2012-08-17T23:51:09.827 回答
0

确保你的数组是 NSMutableArray 的一个实例。如果正确完成,那么您知道它[sports objectAtIndex:[indexPath row]]的计算结果为零(nil/NULL)。尝试查询数组的计数。

于 2012-08-18T04:09:09.270 回答