2014-11-11 9 views
0

그래서, 내가 선택한 셀을 이전 VC로 전달하려고하는데, 그 중 하나를 클릭 할 때마다 항상 "사과"문자열 만 전송됩니다. 이유가 확실하지 않습니다. 어떤 아이디어? 참고 : mySelectedCell 내가didSelectRowAtIndexPath Segue returns junk

@interface ChoicesTableViewController() <UITableViewDelegate, UITableViewDataSource> 
@property (weak, nonatomic) IBOutlet UITableView *myTableView; 
@property (strong, nonatomic) NSMutableArray *items; 
@property (strong, nonatomic) NSMutableDictionary *alphabetizedItems; 

@property (strong, nonatomic) NSArray *unsortedKeys; 
@property (strong, nonatomic) NSArray *sortedKeys; 

@end 

@implementation ChoicesTableViewController 
- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.myTableView.delegate = self; 
    self.myTableView.dataSource = self; 

    self.items = [[NSMutableArray alloc] init]; 
    [self.items addObject:@"Apples"]; 
    [self.items addObject:@"Bread"]; 
    [self.items addObject:@"Butter"]; 
    [self.items addObject:@"Cheese"]; 
    [self.items addObject:@"Eggs"]; 
    [self.items addObject:@"Grapes"]; 
    [self.items addObject:@"Ice Cream"]; 
    [self.items addObject:@"Milk"]; 
    [self.items addObject:@"Oranges"]; 
    [self.items addObject:@"Oreos"]; 
    self.alphabetizedItems = [self alphabetizeItems:self.items]; 

} 

//Segue if the item is tapped 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    self.mySelectedCell = [self.items objectAtIndex:indexPath.row]; 

    [self performSegueWithIdentifier:@"unwindSegueAction" sender:self]; 

} 
//unwind segue from add choice 

- (IBAction)unwindSegueToChoices:(UIStoryboardSegue *)segue 
{ 

    AddChoiceViewController *sourceVC = segue.sourceViewController; 
    NSString *myNewItem = sourceVC.myTextField.text; 
    //NSString *myFinalString = [[myNewItem substringToIndex:1] capitalizedString]; 
    NSString *stringCapitalized = [myNewItem capitalizedString]; 
    [self.items addObject:stringCapitalized]; 
    self.alphabetizedItems = [self alphabetizeItems:self.items]; 
    //[self.arrayNames addObjectsFromArray:@[[MyDataChoices itemWithNewName:stringCapitalized]]]; 
    [self.tableView reloadData]; 

} 
//titles for talble view 
#pragma mark Helper Methods 
- (NSMutableDictionary *)alphabetizeItems:(NSArray *)items { 
    NSMutableDictionary *buffer = [[NSMutableDictionary alloc] init]; 

    for (int i = 0; i < [items count]; i++) { 
     NSString *item = [items objectAtIndex:i]; 
     NSString *firstLetter = [[item substringToIndex:1] uppercaseString]; 

     if ([buffer objectForKey:firstLetter]) { 
      [(NSMutableArray *)[buffer objectForKey:firstLetter] addObject:item]; 

     } else { 
      NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithObjects:item, nil]; 
      [buffer setObject:mutableArray forKey:firstLetter]; 
     } 
    } 

    NSArray *keys = [buffer allKeys]; 
    for (int j = 0; j < [keys count]; j++) { 
     NSString *key = [keys objectAtIndex:j]; 
     [(NSMutableArray *)[buffer objectForKey:key] sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 
    } 

    NSMutableDictionary *result = [NSMutableDictionary dictionaryWithDictionary:buffer]; 
    return result; 
} 
#pragma mark title indexing 

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    NSArray *keys = [[self.alphabetizedItems allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 
    NSString *key = [keys objectAtIndex:section]; 
    return key; 
} 



# pragma mark main table view 
-(NSInteger) numberOfSectionsInTableView:(UITableView *) tableView 
{ 
    NSArray *keys = [self.alphabetizedItems allKeys]; 
    return [keys count]; 
} 


-(NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section 
{ 
    //return self.arrayNames.count; 
    self.unsortedKeys = [self.alphabetizedItems allKeys]; 
    self.sortedKeys = [self.unsortedKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 
    NSString *key = [self.sortedKeys objectAtIndex:section]; 
    NSArray *itemsForSection = [self.alphabetizedItems objectForKey:key]; 
    return [itemsForSection count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //MyDataChoices *currentRow = self.arrayNames[indexPath.row]; 
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"mainCell2" forIndexPath:indexPath]; 

    //cell.textLabel.text = currentRow.myNameChoices; 
    NSString *key = [self.sortedKeys objectAtIndex:[indexPath section]]; 
    NSArray *itemsForSection = [self.alphabetizedItems objectForKey:key]; 
    NSString *item = [itemsForSection objectAtIndex:[indexPath row]]; 

    [cell.textLabel setText:item]; 

    return cell; 

} 

# pragma Mark delete slide button 

//Delete Swipe Button 
// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 



// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     NSArray *unsortedKeys = [self.alphabetizedItems allKeys]; 
     NSArray *sortedKeys = [unsortedKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 
     NSString *key = [sortedKeys objectAtIndex:[indexPath section]]; 
     NSArray *itemsForSection = [self.alphabetizedItems objectForKey:key]; 
     if (itemsForSection.count == 1) { 
      // Delete the whole section 
      [self.alphabetizedItems removeObjectForKey:key]; 
      [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade]; 
     } else { 
      // Delete the row from the data source 
      //int index = indexPath.row; 
      [self.alphabetizedItems removeObjectForKey:indexPath]; 

      [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     } 
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


@end 

에서 내가 데이터를 전달있어하는 .m 파일을 데이터를 전달하기 위해 노력하고있어 ChoicesTableViewController 하는 .m 파일의 .H 파일에 초기화 된있는 NSString입니다

#import "ShoppingListTableViewTableViewController.h" 
#import "myData.h" 
#import "ChoicesTableViewController.h" 

@interface ShoppingListTableViewTableViewController() 

@property (strong, nonatomic) NSMutableArray *itemData; 

@end 

@implementation ShoppingListTableViewTableViewController 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.itemData = [[NSMutableArray alloc] init]; 

    [self.itemData addObjectsFromArray:@[ 
             [myData itemWithName:@"test"]]]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return self.itemData.count; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    myData *currentRow = self.itemData[indexPath.row]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mainCell" forIndexPath:indexPath]; 
    cell.textLabel.text = currentRow.myName; 
    NSString *stringCount = [NSString stringWithFormat:@"%d", currentRow.count]; 
    cell.detailTextLabel.text = stringCount; 

    cell.imageView.image = [UIImage imageNamed:@"Box-NoCheckMark.png"]; 



    return cell; 
} 
//unwind segue data from choicestableviewcontroller 
- (IBAction)unwindSegueAction:(UIStoryboardSegue *)segue 
{ 
    ChoicesTableViewController *vcb = (ChoicesTableViewController *)segue.sourceViewController; 
    BOOL isFound = false; 
    for (myData *item in self.itemData) 
    { 
     if([item.myName isEqualToString:vcb.mySelectedCell]) 
     { 
      item.count ++; 
      isFound = true; 
      break; 
     } 

    } 

    if(!isFound) 
    { 
     [self.itemData addObjectsFromArray:@[[myData itemWithName:vcb.mySelectedCell]]]; 

    } 
    [self.tableView reloadData]; 

} 


//Box checked 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    myData *check; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mainCell" forIndexPath:indexPath]; 
    if(check.checkMark) 
    { 
     cell.imageView.image = [UIImage imageNamed:@"Box-NoCheckMark.png"]; 
     check.checkMark = NO; 
    } 
    else 
    { 
     cell.imageView.image = [UIImage imageNamed:@"Box-CheckMark.png"]; 
     check.checkMark = YES; 
    } 
} 

//Delete Button when Swiped to the left 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 



// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     int index = indexPath.row; 
     [self.itemData removeObjectAtIndex:index]; 

     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 

@end 

답변

-1

정확한 문자열을 얻으려면 cellForRowAtIndexPath에서 사용하는 것과 동일한 논리를 didSelectRowAtIndexPath에 사용해야합니다. [self.items objectAtIndex : indexPath.row]를 사용할 수 없습니다.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *key = [self.sortedKeys objectAtIndex:[indexPath section]]; 
    NSArray *itemsForSection = [self.alphabetizedItems objectForKey:key]; 
    self.mySelectedCell = [itemsForSection objectAtIndex:[indexPath row]]; 
    [self performSegueWithIdentifier:@"unwindSegueAction" sender:self]; 

} 
+0

이것은 완벽하게 작동했습니다. 내가 뭘 생각하는지 모르겠다. 고마워요! – JMH1991