目前,我編輯了一個將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我的陣列,它表明,當我點擊了同一細胞,創建兩個單獨的對象。任何幫助?