目前,我编辑了一个将Exercise对象添加到NSMutableArray的委托函数。但是,我不想添加重复的对象,相反,如果对象已经在数组中,我只想简单地访问该特定对象。将对象插入到NSMutableArray
这里是我的代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text; // Retrieves the string of the selected cell.
Exercise *exerciseView = [[Exercise alloc] initWithExerciseName:str];
WorkoutManager *workoutManager = [WorkoutManager sharedInstance];
if (![[workoutManager exercises] containsObject:exerciseView]) {
[[workoutManager exercises] insertObject:exerciseView atIndex:0];
[self presentModalViewController:exerciseView animated:YES];
NSLog(@"%@", [workoutManager exercises]);
}
else {
[self presentModalViewController:exerciseView animated:YES];
NSLog(@"%@", [workoutManager exercises]);
}
}
我认为这将工作,但是,当我跑我的代码和NSLogged我的阵列,它表明,当我点击了同一细胞,创建两个单独的对象。任何帮助?