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 に答える