向数组添加字符串

时间:2012-08-17 23:47:07

标签: objective-c c

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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

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